【发布时间】:2019-03-26 11:14:10
【问题描述】:
我正在尝试使用 JS 或 jQuery 来访问一个名为“data-button”的 html 值。我可以访问整个 HTML div 并从按钮中提取类名和文本内容,但我无法获取数据按钮值。
在下面的代码中,我有一个 captureRecipeButtons() 函数,它可以获取“recipe-1-container”div。
function captureRecipeButtons(){
let theWholeDiv = document.getElementsByClassName("recipe-1-container")[0];
let buttonValue = ?;
}
<div class="recipe-1-container">
<button class="listed-recipe-link" data-button="1">Element</button>
</div>
在我的 captureRecipeButtons() 函数中,我希望 buttonValue 在上面的代码中等于“1”。任何帮助将不胜感激。
【问题讨论】:
-
能把html发一下吗
-
据我所知,使用 .innerhtml 仅返回一个字符串,您可以使用迭代器来完成所有操作,但使用 .ChildNodes[0] 获取元素的方法更简单您想要编辑或获取或更改,然后在子节点上使用 .innerHTML
标签: javascript jquery html