我有一个 HTML Div(我正在使用 Twitter Bootstrap 隐藏-显示 div2
),如下所示:
HTML:
<div id="div1">
<button id="mybtn">Button</button>
<div id="div2" class="modal hide fade span12" style="display: none;">
<button id="anotherButton" data-char="">AnotherButton</button>
</div>
</div>
当我点击 mybtn
时,我尝试使用 jQuery 更改 data-char
的值。但没有成功,因为 div2
被隐藏了。 console.log($('#anotherButton'));
返回一个空数组。
有没有办法用 jQuery 或 Javascript 访问隐藏的 DOM?
*编辑:*
尝试这样做效果不佳,返回 undefined
:
$('#mybtn').live('click', function(e)
{
//or e.preventDefault();
$('#anotherbutton').attr("data-char", "assigned");
alert($('#anotherbutton').attr("data-char"));
});
最佳答案
可以赋值
$(function(){
$('#anotherButton').attr('char', "assigned");
alert($('#anotherButton').attr('char'));
});
点击按钮
$(function(){
$('#anotherButton').attr('data-char', "assigned");
alert($('#anotherButton').attr('data-char'));
});
关于javascript - 如何使用 jQuery 访问隐藏的 Dom 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11831824/