我使用的 CMS 中的 WYSIWYYG 编辑器正在删除 iframe 代码,以便此代码
<p><iframe src="http://www.example.com" frameborder="0" width="300" height="300" scrolling="no"></iframe></p>
加载到页面时显示如下
<p><iframe src="http://www.example.com" frameborder="0" width="300" height="300" scrolling="no"></iframe></p>
我使用此代码修复了它。
$('p').each(function () {
var $this = $(this);
var tt = $this.text();
$this.html(tt.replace('<', '<').replace('>', '>'));
});
但是,如果我的页面上还有其他标签,例如
<p><strong>Strong text </strong></p>
这也被剥离出来看起来像
<p>Strong text </p>
如何才能使此更改仅适用于 iframe?
我已经尝试过类似的方法,但没有成功。
$this.html(tt.replace('<iframe', '<iframe').replace('></iframe>', '</iframe>'));
最佳答案
我最终通过使用 :contains eg 让它工作
$('p:contains("iframe")').each(function () {
var $this = $(this);
var tt = $this.text();
$this.html(tt.replace('<', '<').replace('>', '>'));
});
不是最好的解决方案,但这适用于网站所追求的目标。
关于javascript - 仅替换 iframe 的编码字符 Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20624568/