我正在使用的CMS中的所见即所得编辑器正在剥离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('>', '>'));
});
不是最好的解决方案,但这适用于网站所追求的内容。
好的,首先,尝试找到更改RTE设置的位置。这是一个常见问题,您应该能够控制允许哪些标签。
如果你真的想用jQuery破解它,你可以创建你的模式,例如:
<p class="iframeMe" data-src="http://iframe-source" data-width="500" data-height="500"> Iframe will appear here </p>
$('.iframeMe').each(function () {
var $this = $(this);
var $iframe = $('<iframe />').attr('src', $this.data('src')).attr('width', $this.data('width')).attr('height', $this.data('height'));
$this.append($iframe);
});
试试这个选择器:
$('iframe').each(function () {
var $this = $(this).parent(); // get its parent
var tt = $this.text();
$this.html(tt.replace('<', '<').replace('>', '>'));
});