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); };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ue5运行的通道视频都会有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数