javascript - 如何使用 Javascript 了解父/容器样式?

标签 javascript jquery html css

我的想法来自:

Can you check an object's CSS display with JavaScript?

现在看下面我的代码:

HTML :

<div style="display: none">
 <input type="text" id="myInput" value="5">
</div>

Javascript:

console.log(document.getElementById('myInput').value);
console.log(document.getElementById('myInput').style.display);

我电脑上的结果在我的浏览器控制台上什么也没有显示。

My problem :

I need to know the parent of input type text style. cause if the parent style is none make the input type text show nothing when i try to get value of input type text. So the main question is how to get parent style WITHOUT add new id/class/name in div?

最佳答案

您可以将 .parentNode 属性与 style 一起使用,以获得您正在寻找的特定 CSS 属性。所有这些都列在对象上。 HTML 将是:

  <div style="display: none">
    <div id='one'>
    </div>
  </div>

javascript 将是:

var node = document.getElementById('one');

console.log(node.parentNode.style.display);

See the fiddle

关于javascript - 如何使用 Javascript 了解父/容器样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43535732/

相关文章:

javascript - Html 和 Internet Explorer : How to avoid hidden elements to be copied/pasted

javascript - 渐进式加载代码片段不太了解

javascript - 使用 HTML5 和 JavaScript 在 SQLite 中插入和检索 BLOB 图像

javascript - Jquery单选复选框问题

javascript - x-editable:取消事件的回调

javascript - 是否可以从 Javascript 返回 CSS 媒体查询声明?

javascript - 使用 jQuery 循环遍历表格 - 计算每行的单元格

javascript - 如果使用 jquery,如何检查文本框是否为空并显示弹出消息?

javascript - 手机定时触摸事件

javascript - 计算js数组中的重复项总数