Javascript 属性中未设置js html类型的文本框值?

Javascript 属性中未设置js html类型的文本框值?,javascript,html,dom,Javascript,Html,Dom,伙计们, 我编写了一些代码来保存网页的当前状态。当有一个文本框,我通过输入来更改值时,更改不会在html代码中注册。即使我打电话: object.value = 'x'; is不会更改该值。当我使用: object.setAttribute('value','x'); 然后在html代码中注册更改 通过文本框循环设置onchange属性不是一个解决方案,因为我有一些特殊的文本框已经设置了onchange属性 谢谢你的帮助 function SaveHTML() { removeThis(ge

伙计们, 我编写了一些代码来保存网页的当前状态。当有一个文本框,我通过输入来更改值时,更改不会在html代码中注册。即使我打电话:

object.value = 'x';
is不会更改该值。当我使用:

object.setAttribute('value','x');
然后在html代码中注册更改

通过文本框循环设置onchange属性不是一个解决方案,因为我有一些特殊的文本框已经设置了onchange属性

谢谢你的帮助

function SaveHTML()
{
removeThis(get('Source'));
var newdiv = document.createElement('div');
newdiv.setAttribute('id','Source');
newdiv.style.position= 'absolute';
newdiv.style.top= (tempY-200)+'px';
newdiv.style.left= (tempX+30)+'px';
newdiv.style.backgroundColor = 'white';
var html=encodeURI(document.getElementsByTagName('body')[0].innerHTML);
newdiv.innerHTML='HTML:<br><textarea rows="20" cols="60">'+html+'</textarea><br><input type=button value=close onclick=removeParent(this)>';
document.body.appendChild(newdiv);
}

function LoadHTML()
{
removeThis(get('Source'));
var newdiv = document.createElement('div');
newdiv.setAttribute('id','Source');
newdiv.style.position= 'absolute';
newdiv.style.top= (tempY-200)+'px';
newdiv.style.left= (tempX+30)+'px';
newdiv.style.backgroundColor = 'white';
newdiv.innerHTML='HTML:<br><textarea id=code rows="20" cols="60"></textarea><br><input type=button value=cancel onclick=removeParent(this)> <input type=button value=load onclick=document.getElementsByTagName(\'body\')[0].innerHTML=decodeURI(get(\'code\').value)>';
document.body.appendChild(newdiv);
}

function get(Element)
{
return document.getElementById(Element);
}

function removeThis(obj)
{ 
try{
obj.parentNode.removeChild(obj);
}
catch (e) {
    return;
}
}

function removeParent(obj)
{
obj.parentNode.parentNode.removeChild(obj.parentNode);
}

嗯,真奇怪。我没想到它会这样。有趣

您只需在保存HTML时循环遍历每个输入字段,然后执行以下操作:

object.setAttribute("value", object.value)
?


您还可以使用大型框架的事件处理功能之一,它们可以附加事件而不覆盖旧的事件。

不应使用setAttribute和getAttribute,因为它们不是标准,使用.value应该可以工作。但另一种方法是使用jQuery的attr方法

$('#myID').attr('id', 'newID');
$('#myID').attr('value', 'newValue');
此外,还应使用适当的引号,如下所示:

newdiv.innerHTML='HTML:<br><textarea rows="20" cols="60">'+html+'</textarea><br><input type="button" value="close" onclick="removeParent(this)">';

在SaveHTML调用中,我运行了这个函数。工作起来很有魅力

function TouchInputs()
{
var everything = document.getElementsByTagName('input');
var everythinglength = everything.length;
for(var i = 0;i<everythinglength;i++)
    {
        try{
            everything[i].setAttribute('value',everything[i].value);
        }
        catch(e){
                alert(e.message);
            }
    }
}

您的问题是为什么setAttribute工作并且o.value=。。。不是吗?我在FF IE Chrome和Safari上测试了行为。只有IE具有预期的行为。我知道JQuery非常常用,但是对于这个项目,我不希望使用它。谢谢你。
$('#myID').onchange(function(){/* your stuff here */});
function TouchInputs()
{
var everything = document.getElementsByTagName('input');
var everythinglength = everything.length;
for(var i = 0;i<everythinglength;i++)
    {
        try{
            everything[i].setAttribute('value',everything[i].value);
        }
        catch(e){
                alert(e.message);
            }
    }
}