Add dynamic text to a p tag
I have a p tag that has some styled text inside it I want to keep that style and when I click a button with Jquery I want to add new text to it. It was all working well until I tried to add a br tag to my text then it displays the br tag as part of the text.
button.click(function(evt){
var td = $(this).closest('td');
date.find('.chosen').text( td.data('day') + ' May ' + td.data('date') + "'</br>'" + $(this)开发者_运维问答.text() );
}
If I use html() it will remove my styling from my p tag.
1 - A line break looks like this: <br />
2 - You are double quoting, it should just be + "<br />" +
3 - You should use .html
instead of .text
for insertion:
button.click(function(evt){
var td = $(this).closest('td');
date.find('.chosen').html( td.data('day') + ' May ' + td.data('date') + "<br />" + $(this).text() );
}
精彩评论