javascript - 如何使用 jQuery 访问隐藏的 Dom 元素?

标签 javascript jquery

我有一个 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"));
});

最佳答案

可以赋值

Live demo

$(function(){
    $('#anotherButton').attr('char', "assigned");
    alert($('#anotherButton').attr('char'));
});​

点击按钮

Live Demo

$(function(){
   $('#anotherButton').attr('data-char', "assigned");
   alert($('#anotherButton').attr('data-char'));
});​

关于javascript - 如何使用 jQuery 访问隐藏的 Dom 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11831824/

相关文章:

javascript - MongoDB:如何创建一个具有对象数组作为字段的对象?

javascript - 事件/非事件状态

javascript - jQuery ajax 请求,PHP 返回 false 但没有错误

jquery - 使用node.js向mysql数据库添加数据

jquery - 连续高度相等?查询

Jquery validate 隐藏 kendo-ui 控件

javascript - 如何在 Svelte 组件中基于 props 定义 CSS 字段?

javascript - CSS 应用于嵌套 li 问题

javascript - Edge 预加载所有 html 媒体资源?页面加载缓慢

javascript - AngularJS,输入文本框甚至不显示