JQuery更改所有元素文本
我需要jquery来改变所有页面中的数字。 例如,我想要改变1比1,所以我尝试这样:
$("*").each(function(){ $(this).html( $(this).html().replace(1,"۱") ); })
但这也将改变css规则和属性。 是否有任何技巧可以逃避css和属性?
这不是jQuery自然适合的工作。 而不是让jQuery获取所有元素的平面列表,而是自己递归遍历DOM树,搜索文本节点以执行替换。
function recursiveReplace(node) { if (node.nodeType == 3) { // text node node.nodeValue = node.nodeValue.replace("1", "۱"); } else if (node.nodeType == 1) { // element $(node).contents().each(function () { recursiveReplace(this); }); } } recursiveReplace(document.body);
在这里看到它。
试试这个:
$("body *").each(function() { if ( this.childElementCount > 0 ) { return; } $(this).text(function(i, v) { return v.replace('1','۱'); }); });
更新:不起作用……
如果没有看到代码的实际示例,很难提供一个好的答案,但我会尽我所能。
这就是我要做的。 我会将所有页码都包含在带有特定ID的标签中,如下所示……
1 ... n
那么这是jQuery ……
$.each($("span#pageNumber"), function()) { $(this).text('value'); });
您可以增加循环中的值以计算页面或您想要的任何其他内容!
希望有所帮助,
spryno724