Javascript 在使用jquery时单击链接标记?
我有一个不包含url的链接标签,但它有javascript。我正在使用jQuery,当我尝试“.click()”函数时,它不起作用。我搜索了整个网站,找到了一些答案,但所有答案都打开了链接标签的“href”,这些答案对我来说没有帮助,下面是一个代码示例:Javascript 在使用jquery时单击链接标记?,javascript,jquery,html,Javascript,Jquery,Html,我有一个不包含url的链接标签,但它有javascript。我正在使用jQuery,当我尝试“.click()”函数时,它不起作用。我搜索了整个网站,找到了一些答案,但所有答案都打开了链接标签的“href”,这些答案对我来说没有帮助,下面是一个代码示例: <button onclick="ClickMe();">CLick Me!</button> <a id="test" href="javascript:alert('hi');">Alert</a&
<button onclick="ClickMe();">CLick Me!</button>
<a id="test" href="javascript:alert('hi');">Alert</a>
<script>
function ClickMe() {
$("#test").click();
}
</script>
点击我!
函数ClickMe(){
$(“#测试”)。单击();
}
这是一个共享项目,我更改页面html的能力非常有限
下面是一个例子:使用普通Javascript
function ClickMe() {
$('#test')[0].click();
}
您正试图触发您尚未绑定的具有id测试的锚定标记的点击。您将警报置于
href
未绑定点击,使用href绑定点击事件。您将能够使用点击()
Html
工作演示
如果你想使用href
像这样使用它
此外,上面的所有答案也包含单击方式
希望这有助于解决问题。:)
代码
function ClickMe() {
window.location.href = $("#test").prop('href');
}
这可能有用
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
alert("hi!");
}
</script>
</head>
<body>
<input type="button" onclick="myFunction()" value="Click Me" />
</body>
</html>
函数myFunction()
{
警惕(“嗨!”);
}
Alert@Mohsen试试这个:)
虽然我把id改成了类,但我已经把一些东西改成了类,但是必须提到索引,因此不是通用的解决方案:)
@Mohsen:1)可以有不同的id名称2)把id改成类3)使用eq。
function ClickMe() {
window.location.href = $("#test").prop('href');
}
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
alert("hi!");
}
</script>
</head>
<body>
<input type="button" onclick="myFunction()" value="Click Me" />
</body>
</html>