javascript - 使用 jquery 遍历 html 页面的所有 anchor 标记?

标签 javascript jquery html css

我想引用页面上所有的 anchor 标签

<body>
    <h1>not me</h1>
    <a href ="google.com"></a>
    <h1>not me also </h1>
    <a href ="fb.com"></a>

    <h2>me also as i am H2 </h2>
    <a href ="FIDDLE.com"></a>
    <h3>not me also </h3>
    <a href ="fbO.com"></a>
    <h2>me also as i am H2 </h2>
    <a href ="FIDDLE2.com"></a>

    <button onclick="score2Dmatrix();" id="btn" ></button>

h2 标签作为他们使用 jquery 的父标签,为此我必须通过每个具有父 h2 的 anchor 标签,然后添加属性

onclick="callme();";

我正在使用该代码 onclick

<script>      
function callme() {
    $("h2 a").each(function () {
    $(this).attr("onclick", "score2Dmatrix();");
    });
};
</script>

但没有努力 fiddle 的链接是这个jsfiddle

感谢托比的回答是正确的

selector(h2 + a)...

最佳答案

请参阅:https://jsbin.com/towejoqudu/edit?html,js,console,output

我已将文本添加到您的 anchor 中,以便您实际点击它们。

关于javascript - 使用 jquery 遍历 html 页面的所有 anchor 标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36193900/

相关文章:

javascript - 将 XML 转换为普通的旧 JavaScript 对象?

jquery - 如何使用jquery添加父标签

javascript - html5播放器不播放

javascript - 将 HTML 表导出到 Excel,包括文本区域和下拉选定值

javascript - 在关闭 debugInfo 的情况下从 angular.element 获取范围的一些方法

javascript - 在 Javascript(Web 应用程序)中读取和使用用户文件中的数据,而不上传它们

jquery - 在 jQuery 中获取最高后代后代

javascript - jQuery 在传递 POST 数据时在新选项卡中打开页面

javascript - 意外的 ionicons 行为

javascript - KOA 中最纯粹的请求