[英]How to add text between html tags in a string client side?
After a call in a library I get the following string在图书馆打电话后,我得到以下字符串
var data = "<td>123.456</td>";
I'd like to insert some data (a <img />
tag) after the content of the <td></td>
and get something like我想在
<td></td>
的内容之后插入一些数据(一个<img />
标签)并得到类似的东西
<td>123.456 <img src='path' /></td>
How to properly do this in JQuery ?如何在 JQuery 中正确执行此操作?
您可以使用以下方法附加所需的数据:
$('td').append('<img src="path">');
If you want to get the modified string in a variable如果要在变量中获取修改后的字符串
var string = '<td>123.456</td>';
//create a temp element with the contents of the given string
var $div = $('<div />', {
html: string
});
//find the td and manipulate it
$div.find('td').append('<img src="path">');
//get the modified content from the temp element
var string2 = $div.html();
console.log(string2)
You could use jQuery for that, but it's easier to just use string operations in plain Javascript:您可以为此使用 jQuery,但在纯 Javascript 中使用字符串操作更容易:
var data = "<td>123.456</td>";
data = data.replace("</td>", " <img src='path' /></td>");
Check the demo http://jsfiddle.net/yeyene/xQh5J/183/检查演示http://jsfiddle.net/yeyene/xQh5J/183/
$(document).ready(function(){
var data = "<td>123.456</td>";
$('#my_table tr').append(data);
$('#add').on('click',function(){
$('#my_table td').html($('td').text()+' <img src="http://www.softicons.com/assets/templates/softicons/images4/winlogo.png" width="16" height="16" />');
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.