限时优惠活动
亲爱的苦力吧用户,我们为了回馈新老用户一直以来的鼎力支持,即日起(2025-02-06至2025-03-06)凡是购买苦力吧VIP/充值K币的新老用户,都直接可获得买一送一的优惠馈赠。例如:购买一个月的VIP会员可直接获得两个月的VIP会员;充值100K币可直接获得200K币,以此类推!有任何疑问可联系在线客服,感谢各位用户对苦力吧素材的信任与厚爱,我们将一如既往的给大家上新更多优质的素材源码,祝大家开工大吉、工作顺利、心想事成。

jquery对比两段文本文字的差异不同突出显示

所属分类: 网页特效-其它&杂项    2023-10-21 03:13:10

jquery对比两段文本文字的差异不同突出显示 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

jquery对比两段文本文字的差异不同突出显示(共3个文件)

    • styles.css
    • index.html
    • app.js

使用方法

var refSentenceValue;
var mySentenceValue;
var output;
var algoReturn
$(document).ready(function () {
    main()
});

function main() {
    console.log("Initializtion")
}

function buttonClick() {

    // Get element values
    refSentenceElement = $("#refSentence").val()
    mySentenceValue = $("#mySentence").val()
    output = $("#output")

    // Get results from the algorithm
    algoReturn = sentenceChangeAlgo(refSentenceElement, mySentenceValue)

    // Split the string
    var mySentenceValueSplitted = mySentenceValue.split(" ")

    // Output HTML
    var myOutputHTML = "Empty";

    // Array of Words
    var myWords = []

    myOutputHTML = ""

    // Add HTML support for new, shifted and normal words
    for (let i = 0; i < mySentenceValueSplitted.length; i++) {
        // Init Word object with empty properties
        var myWord = new Object()
        myWord.value = ""
        myWord.html = ""
        myWord.newWord = false
        myWord.delWord = false
        myWord.shifted = false

        var currentWord = mySentenceValueSplitted[i]
        myWord.value = currentWord
        if (algoReturn.newWords.includes(currentWord)) {
            myWord.html = "<span class='new'>" + currentWord + "</span>"
            myWord.newWord = true
        }

        else if (algoReturn.shiftedWords.includes(currentWord)) {
            myWord.html = "<span class='shifted'>" + currentWord + "</span>"
            myWord.shifted = true
        }
        else {
            myWord.html = "<span class='normal'>" + currentWord + "</span>"
        }
        myWords.push(myWord)
    }

    // Add deleted words in the final string
    // Loop through the deleted words
    for (let i = 0; i < algoReturn.delWordsIndices.length; i++) {
        var myWord = new Object()
        myWord.value = algoReturn.delWords[i]
        myWord.html = "<span class='del'>" + algoReturn.delWords[i] + "</span>"
        myWord.delWord = true
        myWords.splice(algoReturn.delWordsIndices[i], 0, myWord)

    }

    myWords.map((item) => {
        myOutputHTML += item.html + " "
    })
    output.html(myOutputHTML)

}

function sentenceChangeAlgo(string1, string2) {
    // Tokenization based on space only
    var string1Splitted = string1.split(" ")
    var string2Splitted = string2.split(" ")

    // Variables to save settings
    var newWords = []
    var newWordsIndices = []

    var delWords = []
    var delWordsIndices = []

    var shiftedWords = []
    var shiftedWordsIndices = []

    // Check for new words
    // Loop through the first string
    for (let i = 0; i < string2Splitted.length; i++) {
        var currentWord = string2Splitted[i]
        var isFound = false
        // Loop through the second string
        for (let j = 0; j < string1Splitted.length; j++) {
            // Word found
            if (currentWord == string1Splitted[j]) {
                isFound = true
                break
            }
        }
        // Word not found
        if (isFound == false) newWords.push(currentWord)
    }

    // Check for deleted words 
    // Loop through the first string
    for (let i = 0; i < string1Splitted.length; i++) {
        var currentWord = string1Splitted[i]
        var isFound = false
        // Loop through the second string
        for (let j = 0; j < string2Splitted.length; j++) {
            // Word found
            if (currentWord == string2Splitted[j]) {
                isFound = true
                break
            }
        }
        // Word not found
        if (isFound == false) delWords.push(currentWord)
    }


    // Check for shifted words
    // Loop through the first string
    for (let i = 0; i < string1Splitted.length; i++) {
        var currentWord = string1Splitted[i]


        // Loop through the second string
        for (let j = 0; j < string2Splitted.length; j++) {
            // Word found
            if (currentWord == string2Splitted[j]) {
                if (i != j) {
                    shiftedWords.push(currentWord)
                    shiftedWordsIndices.push(j)
                }
                break
            }
        }

    }

    // Detect new words indices
    for (let i = 0; i < newWords.length; i++) {
        // Loop through the second string
        for (let j = 0; j < string2Splitted.length; j++) {
            // Word found
            if (newWords[i] == string2Splitted[j]) {
                newWordsIndices.push(j)
                break
            }
        }
    }

    // Detect deleted words indices 
    for (let i = 0; i < delWords.length; i++) {
        // Loop through the second string
        for (let j = 0; j < string1Splitted.length; j++) {
            // Word found
            if (delWords[i] == string1Splitted[j]) {
                delWordsIndices.push(j)
                break
            }
        }
    }

    // console.log("new words:", newWords)
    // console.log("new words indices:", newWordsIndices)
    // console.log("deleted words:", delWords)
    // console.log("deleted words indices:", delWordsIndices)
    // console.log("shifted words:", shiftedWords)
    // console.log("shifted words indices", shiftedWordsIndices)

    var algoReturn = new Object()
    algoReturn.newWords = newWords
    algoReturn.delWords = delWords
    algoReturn.shiftedWords = shiftedWords
    algoReturn.shiftedWordsIndices = shiftedWordsIndices
    algoReturn.newWordsIndices = newWordsIndices
    algoReturn.delWordsIndices = delWordsIndices
    return algoReturn
}

站长提示:
1. 苦力吧素材官方QQ群:950875342
2. 平台上所有素材资源,需注册登录会员方能正常下载。
3. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励2K币
4. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
5. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 其它&杂项

jquery实现的tooltip悬停提示信息插件

带10多种鼠标悬停提示信息的tip插件,利用了鼠标hover事件触发。
  其它&杂项
 4358  0

jquery响应式侧边栏固定悬浮可见特效代码

sidebarFix.js是一个轻量级侧边栏插件,在页面滚动时,右侧边栏模块始终可见。
  其它&杂项
 3370  0

jquery输入域名生成二维码扫一扫代码

核心主要运用了qrcode二维码生成插件,网址生成二维码功能,非常实用!
  其它&杂项
 9461  0

jquery检测当前用户空闲状态触发事件

一款当前用户空闲检测插件,用于检测用户的页面的行为状态,并在用户空闲一定时间后触发事件。包含:鼠标操作、键盘操作、触摸操作等。
  其它&杂项
 1307  0

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论