开发者

.replace problem

I have a problem with 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>

what is wrong in my script?


Why are you using regex for this?

If you want to replace an element with another, you can use jQuery's .replaceWith() method.

jQuery(".post_meta_front p:contains('Beschreibung:')")
                                               .replaceWith('<span></span>');

Or if you need to ensure an exact match on the content:

jQuery(".post_meta_front p").filter(function() {
    return $.text([ this ]) === 'Beschreibung:';
}).replaceWith('<span></span>');


You need to escape forward slashes / in the regex part.

<script type="text/javascript">
  jQuery(function() { 
  jQuery(".post_meta_front").html(jQuery(".post_meta_front").html().replace(/<p>Beschreibung:<\/p> /g, '<span></span>'));
});
</script>


It looks like you're not escaping all of the special characters in the find parameter of the replace function. You're only escaping the first < character.

Try something like:

/\<p\>Beschreibung:\<\/p\>/g

Note that replace is a function of javascript not jQuery.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜