简体   繁体   English

jQuery str.replace无法正常工作

[英]jQuery str.replace not working

I need to search for a string (video embed url) once the page loads and replace the & with & 页面加载后,我需要搜索字符串(视频嵌入网址), &&替换& . This is the best guess for code that I have tried but it seems to just have no effect: 这是我尝试过的代码的最佳猜测,但似乎没有效果:

(function ($, document, undefined) {
    $(document).ready(function(){
        var str = $( "iframe" ).attr( "src" );
        str = str.replace(/&/g,"&");
        $( "iframe" ).attr( "src",str );
    });
})(jQuery, document);

There is only once iframe on the page and it looks like this: 该页面上只有一个iframe ,它看起来像这样:

<iframe src='http://player.vimeo.com/video/72874423?title=0&byline=0&portrait=0;api=1' width='615' height='355' style='width:615px;height:355px;'></iframe>

What am I doing wrong? 我究竟做错了什么?

The problem is that once the javascript manipulates the dom, the &amp; 问题在于,一旦javascript操作了dom, &amp; is encoded as HTML back to just an & . 被编码为HTML并返回一个& The actual javascript to replace & with &amp; &amp;替换&的实际JavaScript &amp; works fine. 工作正常。

See this fork of the commenter's JSFiddle. 参见评论者的JSFiddle的这个分支 when the Alert happens, the string has &amp; 发生警报时,字符串中包含&amp; , but once it's written to the document, it displays just & ,但将其写入文档后,仅显示&

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM