不能触发点击href(强制点击href)
Cannot trigger click on href(forcing href to clicked)
我刚刚制作了一个脚本,当我试图在文档加载时自动单击href时,触发器没有发生。我希望href已经被自动点击,但我没有得到触发器。这是我的html
<li style="width: 25%; max-width: 320px; max-height: 200px;">
<a href="#" class="linkProxy"><img src="index_files/small/shirt-4.jpg" data-largeimg="index_files/large/shirt-4.jpg" /></a>
</li>
这是我的Jquery
<script type="text/javascript">
$(document).ready(function(){
$('button.linkProxy').click(function(){
location.href = $('#' + $(this).attr('rel')).attr('href');
});
});
</script>
你能帮助我,因为我是初学者jquery和javascript。提前感谢,如果有其他方法,请建议。
$('button.linkProxy')
将搜索类为linkProxy
和的<button>
元素将不匹配任何元素,因为没有<button>
在你的html。
您需要使用linkProxy
类瞄准<a>
标记,如下所示:
$(document).ready(function(){
$('a.linkProxy').click(function(){
location.href = $('#' + $(this).attr('rel')).attr('href');
});
$('a.linkProxy').trigger('click');
});
还要注意,没有rel
属性在你的html
, idk你想用它做什么,因为你还没有解释它的问题…
听起来你想要重定向。"自动点击"就是这样。
您可以尝试以下两种方法中的一种来模拟页面加载时的"自动点击"。
window.location.replace("http://stackoverflow.com");
window.location.href = "http://stackoverflow.com";
尝试:
<script type="text/javascript">
$(document).ready(function(){
$('button.linkProxy').click(function(){
location.href = $('#' + $(this).attr('rel')).attr('href');
});
$('button.linkProxy').trigger('click');
});
</script>
你只是在你的代码中定义了如何解释点击事件,你实际上并没有做"点击"。
相关文章:
- Selenium 和 Python 不能使用 .click() 点击 href=“javascript:void(0);
- setTimeout window.open 不能接受这个.href
- AngularJs的ui-router ui-sref不能从嵌套状态中生成带有参数的href
- 不能使用JQuery更改href值
- href="javascript:proDoc(..不能在chrome上工作,但IE
- Angularjs的路由不能使用base href
- 锚href不能正确显示
- 不能触发点击href(强制点击href)
- JQuery .html不能在href中添加变量值
- window.location.href不能在Windows Phone 7.1/7.5上工作
- window.location.href不能在IE9下工作
- 取消表单提交后,windows .location.href仍然不能工作
- onclick =“$ . post (this.href);返回false;“不能正常工作
- 输入按钮类型="image"在ie11中不能使用href
- 不能用jquery更改href中的url
- Href 不能正常工作
- jquery窗口.Onbeforeunload不能与href=javascript函数一起工作
- top.location.href不能在ie8中工作
- 不能在jquery移动上使用脚本href = #page-index更改页面
- 为什么 window.location.href 不能在错误页面上工作