doujiao1814 2013-04-14 14:09
浏览 36
已采纳

IE中的JavaScript问题

I have written a code in javascript which creates a p attribute dynamically along with a button which will remove the content. The function removeValue works fine in Chrome and Firefox but does not work in IE. Also, the setAttribute('style','') is not working in IE either. Lastly when I send the values to another page using window.location it sends undefined instead of the text.

Everything seems to work fine in Firefox and Chrome but I can't get it to work in IE (Currently using IE 7). How can I solve this issue?

The code:

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;
    }
}

Turns out that IE supports 'innerText' and firefox supports 'textContent'. I fixed the undefined issue by using 'if' statement. Code updated

  • 写回答

1条回答 默认 最新

  • doucheng7234 2013-04-14 14:44
    关注

    IE has an issue with the setAttribute function and event handlers. (Read here.) Use the onclick property in addition to setAttribute.

    buttonnode.setAttribute('onclick','removeValue(this)');
    buttonnode.onclick = function() { removeValue(buttonnode); };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题