我试图<span>使用jQuery 在文本内部包装文本.以下代码无效.请指出我哪里出错了.还请建议一个更好/不同的方法.
var mysearch = '(05/13/2012-11/13/2012)';
$('table:eq(0) tr:eq(1) th:contains("' + mysearch + '")').filter(function () {
return this.html().replace('(05/13/2012-11/13/2012)', '<span class = "red"> (05/13/2012-11/13/2012)</span>');
});
Run Code Online (Sandbox Code Playgroud)
this在你的代码中是一个没有html方法的DOM Element对象,也就是你不需要filter方法,你可以使用html方法.
$('table:eq(0) tr:eq(1) th:contains("' + mysearch + '")').html(function(_, html) {
return html.replace('(05/13/2012-11/13/2012)', '<span class="red"> (05/13/2012-11/13/2012)</span>');
});
Run Code Online (Sandbox Code Playgroud)
如果该内容th等于mysearchstring,您还可以使用wrapInner方法:
$('table:eq(0) tr:eq(1) th').filter(function(){
return $.trim($(this).text()) === mysearch;
}).wrapInner('<span class="red"></span>');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
192 次 |
| 最近记录: |