iframe正在添加代码,需要将其删除 [英] iframe is adding tags, need to remove them

查看:79
本文介绍了iframe正在添加代码,需要将其删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取iframe的内容(转换为简单的RTF编辑器),然后使用jquery修改这些内容,但是我遇到了问题,因为在IE和FF中,当我获取内容时会添加一个额外的内容带有样式的span标签和div标签.如何删除这些标签/样式.

Im trying to get the contents of an iframe (converted to a simple rich text editor) and then modify those contents using jquery, but I am running into problems because in IE and in FF when I get the content it adds an extra span tag and div tag with stylings. How can I remove these tags/styles.

这就是我获取内容的方式...

This is how Im getting the contents...

var frame = window.frames['richTextField'].document.body.innerHTML; /////Gets contents of iframe
$("#songRes").html(frame); /////Places info into proper div

我正在尝试确定第一个字符是否为'{',如果是,则执行某些操作

Im trying to find out if the first char is a '{' and if so then do something

var frame = window.frames['richTextField'].document.body.innerHTML; /////Gets contents of iframe
  adj = frame.replace(/<div>/gi,"<br \/>").replace(/<\/div>/gi,""); /////String replaces contents of frame
  $("#songRes").html(adj+'<hr />'); /////Places info into proper div
  var array = adj.split("<br />"); /////Splits each line into array values
  var array = frame.split("<br />");
  for(var i = 0; i < array.length; i++){
    var str = array[i];
    if(str.charAt( 0 ) != '{'){/**/}else{/**/} // 
  }

但是问题在于,它会将起始字符显示为<"从我没有添加的span标签中删除.

But the problem is that the it shows the starting char as '<' from the span tag that I didn't add.

推荐答案

我发现了一个惊人的网站,该网站基本上重写了javascript的php代码.我从那里开始使用strip_tags函数.看看...

I found an AMAZING website that basically rewrites php code for javascript. I used the strip_tags function from there. Check it out...

function strip_tags (input, allowed) {
    allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
    var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
        commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
    return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
        return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
    });
}

在...上找到它 phpjs.org

这篇关于iframe正在添加代码,需要将其删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆