简体   繁体   中英

How to formal paragraph text via jquery?

I'm trying to create a syntax highlighter that reviews code and adds bolding to certain keywords. I attempted to replace a given keyword with <b> keyword </b> , but the <b> tags were actually visible in the paragraph. How can I actually format the text? This is my code.

var textbox = $("#textbox")
var code = $("#code")

var patterns = {
    "<b> </b>": ["local", "function"]
}

function format(text) {
    for (var pattern in patterns) {
        for (var keyword in pattern) {
            text = text.replace(patterns[pattern][keyword], pattern.replace(" ", patterns[pattern][keyword]))
        }
    }
    return text
}

textbox.change(function() {
    code.text(format(textbox.val()))
})

Your problem is you are using text() method which will treat any html entities as text also.

Use html() instead since you are including html tags

textbox.change(function() {
    code.html(format(textbox.val()))
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM