stu Id: <input type="text" id="stuid" /> stu Name: <input type="text" id="stuname" />
<a href="<%=request.getContextPath()%>/heafitblog" id="link1" onclick="myFunction()">link</a>
</body>
<script>
function myFunction() {
var eleid = document.getElementsByTagName('a')[0].id;
alert(eleid);
}
</script>
我已经尝试过这个。我能够获取 id,但我不知道如何将 eleid 变量的值传递给调用的路径,即 ../heafitblog 并在那里获取 id。我想在不隐藏的情况下做到这一点输入作为我的 anchor 标记不在表单内。
最佳答案
您可以尝试这种方式,点击后将元素传递给函数,然后调用getAttribute方法获取元素的ID
请查看 anchor 标记和 script 标记上提到的 onclick 方法
请引用以下代码:
stu Id: <input type="text" id="stuid" /> stu Name: <input type="text" id="stuname" />
<a href="<%=request.getContextPath()%>/heafitblog" id="link1" onclick="myFunction(this)">link</a></body>
<script>
function myFunction(element) {
var eleid = element.getAttribute('id');
alert(eleid);
}
</script>
关于javascript - 如何在点击时获取 anchor 标记的 id 并将其传递到 servlet 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61848108/