Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 删除ie8中由于额外(系统默认)属性而无法正常工作的所有属性_Javascript_Jquery_Attributes_Internet Explorer 8 - Fatal编程技术网

Javascript 删除ie8中由于额外(系统默认)属性而无法正常工作的所有属性

Javascript 删除ie8中由于额外(系统默认)属性而无法正常工作的所有属性,javascript,jquery,attributes,internet-explorer-8,Javascript,Jquery,Attributes,Internet Explorer 8,我有一个函数,可以删除所有标签中的所有属性,除了像colspan、color或align这样的少数属性 当我向这个函数发送HTML代码时,它在Firefox和IE9中运行良好,但在IE8和IE7中,它发现了很多元素没有的属性。例如,当我发送: jRemoveAtt("<button color=\"red\" id=\"start\">Hello World</button>") jRemoveAtt(“你好世界”) 它应该找到color属性并跳过它,然后找到id属性

我有一个函数,可以删除所有标签中的所有属性,除了像colspan、color或align这样的少数属性

当我向这个函数发送HTML代码时,它在Firefox和IE9中运行良好,但在IE8和IE7中,它发现了很多元素没有的属性。例如,当我发送:

jRemoveAtt("<button color=\"red\" id=\"start\">Hello World</button>")
jRemoveAtt(“你好世界”)
它应该找到color属性并跳过它,然后找到id属性并删除它

但在IE8中,它可以找到更多的属性,如onwrite、onnwrite、onpage、onbeforeactivate等

此函数用于清理HTML并将其发送到可打印的新窗口。它的清理速度非常快,但在IE8中需要8-9秒,同时使浏览器没有响应

我不知道如何才能忽略HTML字符串中未写入的属性。有什么想法吗

以下是我的功能:

function jRemoveAtt(x){
    if(!x)return '';
    var str=$('<div>'+x+'</div>');
    $('*',str).each(function(){
        var c=$(this);
        var attributes = $.map(this.attributes, function(item) {
            var a=item.name.toLowerCase();
            /*alert(a); */  //this alert shows the extra tags when activated
            if (a!='align'&&a!='colspan'&&a!='span'&&a!='color'&&a!='size') {
                c.removeAttr(item);
            }

        });

    });
    return $(str).html();
};
函数jRemoveAtt(x){
如果(!x)返回“”;
var str=$(''+x+'');
$('*',str).each(函数(){
var c=$(本);
var attributes=$.map(this.attributes,函数(项){
var a=item.name.toLowerCase();
/*警报(a);*///此警报在激活时显示额外的标记
如果(a!='align'&&a!='colspan'&&a!='span'&&a!='color'&&a!='size'){
c、 移除(项目);
}
});
});
返回$(str.html();
};

这样如何:您正在删除元素的所有属性,对吗?与其删除它的所有属性,不如这样做:

HTML:

这是一个测试。
jQuery:

var before = $("#myElement");
var after = $("<"+before[0].tagName+"/>", { html: before.html() });

alert( "Before: "+before[0].outerHTML );
alert( "After: "+after[0].outerHTML );
var-before=$(“#myElement”);
var after=$(“”,{html:before.html()});
警报(“Before::+Before[0].outerHTML);
警报(“之后:”+After[0].outerHTML);
同样的事情,但用另一种方式

编辑:

在可以设置“良好”属性数组的功能中添加

var attrObj = function(el, good){
    var obj = {}, attribs = el[0].attributes;
    var tLC = function(s){ return s.toLowerCase() };

    for(var i=0; i<attribs.length; i++){
        var isGd = false;
        for(var i2=0; i2<good.length; i2++){
            if(tLC(attribs[i].name) == tLC(good[i2])){ isGd = true; break; }
        }
        if(isGd){ obj[attribs[i].name] = attribs[i].value; }
    }

    return obj;
};

var before = $("#myElement");
var newAttr = attrObj(before, ["id"]);
newAttr.html = before.html();

var after = $("<"+before[0].tagName+"/>", newAttr);

alert( "Before: "+before[0].outerHTML );
alert( "After: "+after[0].outerHTML );
var attrObj=函数(el,良好){
var obj={},attribs=el[0]。属性;
var tLC=函数{返回s.toLowerCase()};

对于(var i=0;i您可以在纯JS中执行此操作

HTML:

你好,世界
JS:

var test=document.getElementById(“开始”);
purgeAttributes(测试);
邮寄(测试);
函数purgeAttributes(el)
{
//保留这些属性
var白名单=[“colspan”,“color”,“align”];

对于(var i=0;i在我的评论上展开的内容,类似这样的内容应该可以起到清除的作用:

function purgeAttributes(el) {
    var curIndex = 0;
    var whitelist = ["colspan", "color", "align"];
    var initialLength = el.attributes.length;
    var whiteListCheck = false; 

    for (var i = 0; i < initialLength; i++) {
        var attr = el.attributes.item(curIndex);
        for(var j = 0; j < whitelist.length; j++) {
            if(attr.nodeName === whitelist[j]) {
                whiteListCheck = true;
                // We know that there is an item at curIndex we want to keep, proceed to the next
                curIndex++;
                break;
            }   
        }
        if(!whiteListCheck) {
            el.removeAttribute(attr.nodeName);
        }
    };
}
函数purgeAttributes(el){
var-curIndex=0;
var白名单=[“colspan”,“color”,“align”];
var initialLength=el.attributes.length;
var-whiteListCheck=false;
对于(变量i=0;i
事实上,我需要一些属性。我用这个函数编辑整个页面的html代码,所以我真的需要一些标签,比如Colspan。我的代码在几天内没有出现错误。我不知道为什么它现在让我在ie8中出问题。我真的不想用另一个函数来更改我的函数,我很难让它正常工作。但在这种情况下,我想我没有运气找到更好的/快速的解决方案。我会按照你的建议工作。希望不会再出现问题,并进行修复。我会让你知道。感谢你花时间使用
el.attributes.item(i)访问属性
。假设您在
i=0
时删除了属性。现在您删除了属性,项目数组不会重置其键吗?因此,
项目(1)
中的项目现在位于
项目(0)
,并且由于您正在递增
i
,第二个属性将不会被检查。在FF、Chrome中测试,不确定它在IE或Safari中会有什么反应。
<div color="red" id="start" align="center">Hello World</div>
var test = document.getElementById("start");
purgeAttributes(test);
postAttributes(test);

function purgeAttributes(el)
{
    //keep these attributes
    var whitelist = ["colspan", "color", "align"];
    for(var i=0;i<el.attributes.length;i++)
    {
        var attr = el.attributes.item(i);
        var whiteListCheck = false;
        //loop through whitelist
        for(var j=0;j<whitelist.length;j++)
        {
            if(attr.nodeName === whitelist[j])
            {
                whiteListCheck = true;
                break;
            }   
        }
        if(!whiteListCheck)
        {
            //second parameter means a case-sensitive search
            el.removeAttribute(attr.nodeName, 1);   
        }
    }  
}

function postAttributes(el)
{
    var txtHolder = document.createElement("p");
    var titleTxt = document.createTextNode("attributes: ");
    document.body.appendChild(txtHolder);
    txtHolder.appendChild(titleTxt);
    for(var i=0;i<el.attributes.length;i++)
    {
        var attr = el.attributes.item(i);
        if(attr.nodeValue)
        {
            var txt = document.createTextNode(attr.nodeName + ": " + attr.nodeValue + " ");
            txtHolder.appendChild(txt);
        }
    }  
}
function purgeAttributes(el) {
    var curIndex = 0;
    var whitelist = ["colspan", "color", "align"];
    var initialLength = el.attributes.length;
    var whiteListCheck = false; 

    for (var i = 0; i < initialLength; i++) {
        var attr = el.attributes.item(curIndex);
        for(var j = 0; j < whitelist.length; j++) {
            if(attr.nodeName === whitelist[j]) {
                whiteListCheck = true;
                // We know that there is an item at curIndex we want to keep, proceed to the next
                curIndex++;
                break;
            }   
        }
        if(!whiteListCheck) {
            el.removeAttribute(attr.nodeName);
        }
    };
}