Jes*_*dge 7 javascript jquery dynamic
如何让DOM反映修改后的输入值?
<div>
<input value='0'>
</div>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js'></script>
<script>
setInterval(function() {
$('input').val(parseInt($('input').val()) + 1)
console.log('div.html(): ', $('div').html())
}, 1000)
</script>
Run Code Online (Sandbox Code Playgroud)
尝试直接更改DOM.例如:
<div id="myDiv">
<input id="myInput" value='0'>
</div>
<script>
setInterval(function() {
var v = parseInt(document.getElementById("myInput").value) + 1;
document.getElementById("myInput").setAttribute("value",v);
}, 1000);
</script>
<input type="button" onclick="javascript:alert(document.getElementById('myDiv').innerHTML);" value="Click to see DOM" />
Run Code Online (Sandbox Code Playgroud)
以下似乎对我有用:
这一切都归功于 gnarf 和他的 formhtml:
Firefox 中的 jQuery html() (使用 .innerHTML)忽略 DOM 更改
所以你修改后的代码将是:
<div>
<input value='0'>
</div>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js'></script>
<script>
(function($) {
var oldHTML = $.fn.html;
$.fn.formhtml = function() {
if (arguments.length) return oldHTML.apply(this,arguments);
$("input,button", this).each(function() {
this.setAttribute('value',this.value);
});
$("textarea", this).each(function() {
// updated - thanks Raja!
this.innerHTML = this.value;
});
$("input:radio,input:checkbox", this).each(function() {
// im not really even sure you need to do this for "checked"
// but what the heck, better safe than sorry
if (this.checked) this.setAttribute('checked', 'checked');
else this.removeAttribute('checked');
});
$("option", this).each(function() {
// also not sure, but, better safe...
if (this.selected) this.setAttribute('selected', 'selected');
else this.removeAttribute('selected');
});
return oldHTML.apply(this);
};
//optional to override real .html() if you want
// $.fn.html = $.fn.formhtml;
})(jQuery);
setInterval(function() {
$('input').val(parseInt($('input').val()) + 1)
console.log('div.html(): ', $('div').formhtml())
}, 1000)
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9758 次 |
| 最近记录: |