나는 콘텐츠를 제거 할 버튼과 함께 p
속성을 동적으로 생성하는 코드를 javascript로 작성했습니다. removeValue
함수는 Chrome 및 Firefox에서 제대로 작동하지만 IE에서는 작동하지 않습니다. 또한 setAttribute('style','')
은 IE에서도 작동하지 않습니다. 마지막으로 window.location
을 사용하여 다른 페이지로 값을 보내면 텍스트 대신 undefined
이 전송됩니다.IE에서 JavaScript 문제
Firefox와 Chrome에서 모든 것이 잘 작동하는 것으로 보이지만 Internet Explorer 7 (현재 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
'와 파이어 폭스 'textContent
'지원 밝혀졌습니다. 'if
'문을 사용하여 undefined
문제를 해결했습니다. 코드 업데이트 됨
'window.location' 문제에 대해서는 location이 객체이기 때문에'window.location.href'를 설정해야합니다. – Uby
Dom 액세스가 이상하게 혼합되었습니다. 텍스트 필드의 가치를 원한다면 왜 입력에 액세스하지 않습니까? – mplungjan
@uby 사실이지만 상관 없습니다. 기본 동작은 href를 설정하는 것과 동일하게 수행합니다. – mplungjan