我对html().replace有个问题
<script type="text/javascript">
jQuery(function() {
jQuery(".post_meta_front").html(jQuery(".post_meta_front").html().replace(/\<p>Beschreibung:</p> /g, '<span></span>'));
});
</script>我的脚本出了什么问题?
发布于 2010-09-30 08:21:47
为什么要使用正则表达式呢?
如果你想用一个元素替换另一个元素,你可以使用jQuery's .replaceWith() method。
jQuery(".post_meta_front p:contains('Beschreibung:')")
.replaceWith('<span></span>');或者,如果您需要确保与内容完全匹配:
jQuery(".post_meta_front p").filter(function() {
return $.text([ this ]) === 'Beschreibung:';
}).replaceWith('<span></span>');发布于 2010-09-30 08:18:38
您需要在正则表达式部分转义正斜杠/。
<script type="text/javascript">
jQuery(function() {
jQuery(".post_meta_front").html(jQuery(".post_meta_front").html().replace(/<p>Beschreibung:<\/p> /g, '<span></span>'));
});
</script>发布于 2010-09-30 08:16:49
看起来您并没有转义replace函数的find参数中的所有特殊字符。您只能转义第一个<字符。
尝试如下所示:
/\<p\>Beschreibung:\<\/p\>/g注意,replace是javascript的函数,而不是jQuery的函数。
https://stackoverflow.com/questions/3826823
复制相似问题