简体   繁体   中英

how to include html inside of javascript

i have javascript jquery that looks like this:

var userInput = prompt("Liquid Handler #:", "Liquid Handler #:");
$(this).next('a').text('Liquid Handler #:' + userInput);

i would like to know how i can include html into the text something like this:

var userInput = prompt("Liquid Handler #:", "Liquid Handler #:");
$(this).next('a').text('Liquid Handler #:' + '<b>' + userInput + </b>);

how do i inject html into the javascript?

使用.html而不是.text

$(this).next('a').html('Liquid Handler #:<b>' + userInput + '</b>');

jQuery的.html()方法应该有所帮助。

You could change your .text to .html but bear in mind this is prone to problems in the long run seeing as you'll have to escape anything that goes through it. A better way might be to do something like this:

$(this).next('a').text('Liquid Handler #:').append($("<b>").text(userInput));

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