javascript - 使用 DOM 通过父元素属性访问子元素属性

标签 javascript dom

我试图显示“select”标签中的子元素属性,并使用 document.getElementsByTagName 命令来执行此操作,我期望的显示是“john dalton”,如下所示! ,但浏览器没有像我期望的那样显示警报中的未定义消息。我的来源是这样的:

<select  style=""  name="provinsi_id" class="form-control crud-edit 
  lookup-refresh" onchange="showoption();">
<option>john dalton</option>
<option>john rambo</option>
<script>
alert(document.getElementsByTagName('select')[0].childNodes[0].value);
<script>

最佳答案

试试这个,

alert(document.getElementsByTagName('select')[0].childNodes[1].value);

第 0 个元素是文本元素。

Note: Whitespace inside elements is considered as text, and text is considered as nodes. Comments are also considered as nodes.

关于javascript - 使用 DOM 通过父元素属性访问子元素属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29642311/

相关文章:

javascript - 任何可用于自动同步 .js 文件的工具

javascript - 如何运行作为 DOM 元素中的属性嵌入的脚本?

php - Xpath并在同一表行中选择不同的信息

javascript - React - 渲染没有返回任何内容

javascript - 如何在 JavaScript 循环中添加不同的延迟?

javascript - 我从 .val()-function 得到空字符串 '' 或 'undefined'

javascript - 如何从 jQuery 检索以 JS 点表示法存储的数据?

html - 使用 jQuery 将事件绑定(bind)到动态添加到 DOM 的元素

javascript - 如何在 promise 链内返回promise.map(bluebird)?

javascript - 为什么jquery脚本没有将@Model传递给 Controller ​​的action参数?