作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有谁知道我在哪里犯了错误?
function functionTest(name) {
var Name = name;
alert(Name);
}
<h3 id="name" value="testingName"><a href="#"> test </a></h3>
<button onclick="functionTest(document.getElementById('name').value)"></button>
输出未定义
最佳答案
<h3>
元素没有有 value
属性。但你可以使用data-*
属性来代替。像这样的事情:
<h3 id="name" data-value="testingName">
然后您可以从 dataset
访问它属性:
document.getElementById('name').dataset.value
Example ,使用您的原始代码
function functionTest(name) {
var Name = name;
alert(Name);
}
<h3 id="name" data-value="testingName"><a href="#"> test </a></h3>
<button onclick="functionTest(document.getElementById('name').dataset.value)"></button>
关于javascript - 从 id 获取值并将其传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42633321/
我是一名优秀的程序员,十分优秀!