繁体   English   中英

IE中的JavaScript问题

[英]Problems with JavaScript in IE

我用JavaScript编写了代码,该代码动态创建了p属性以及将删除内容的按钮。 函数removeValue在Chrome和Firefox中工作正常,但在IE中不起作用。 另外, setAttribute('style','')也无法在IE中运行。 最后,当我使用window.location将值发送到另一个页面时,它发送的是undefined而不是文本。

一切似乎都可以在Firefox和Chrome中正常运行,但是我无法在IE(目前使用IE 7)中运行。 我该如何解决这个问题?

编码:

function removeValue(ob)
{
    ob.parentNode.parentNode.removeChild(ob.parentNode);
}
function throwval(obj)
{
    var sent_id = obj.id;     //get id of the button 
    var v = document.getElementById(sent_id).value;
    var newp = document.createElement("p");     //create a new <p> tag
    var text = document.createTextNode(v);
    var buttonnode= document.createElement('input');
    buttonnode.setAttribute('type','button');
    buttonnode.setAttribute('name','del');
    buttonnode.setAttribute('value','Remove');
    buttonnode.setAttribute('style','background-color: Transparent;width: 125;color: blue;border:0');
    buttonnode.setAttribute('onclick','removeValue(this)');
    newp.appendChild(text);
    newp.appendChild(buttonnode);
    document.getElementById("getselected").appendChild(newp);    //append the new <p> tag in the div    
}
function sendvalues()
{
    var div_val = document.getElementById("getselected");
    if(!div_val.getElementsByTagName("p").length)
    {
        alert("Select a value");
    }
    else
    {
        //get seperate values of the paragraph inside div
        var str="|";
        for (i=0; i < div_val.getElementsByTagName("p").length; i++)
        {
            var paragraphs = div_val.getElementsByTagName("p");
            if(!paragraphs.item(i).textContent)
        {
            var pvalues = paragraphs.item(i).innerText;
        }
        else 
        {
            var pvalues = paragraphs.item(i).textContent;
        }
            //var sendpvalues = "products=" + pvalues;
            // alert(pvalues);

            str = str + pvalues + "|";
            //alert (str);
            //ajaxOb.send(sendpvalues);

        }   
        // alert(str);
        window.location="send_data.php?str="+str;
    }
}

事实证明IE支持“ innerText ”,而Firefox支持“ textContent ”。 我通过使用' if '语句解决了undefined问题。 代码已更新

IE的setAttribute函数和事件处理程序存在问题。 在这里阅读。)除了setAttribute之外,还使用onclick属性。

buttonnode.setAttribute('onclick','removeValue(this)');
buttonnode.onclick = function() { removeValue(buttonnode); };

暂无
暂无

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

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