如何像一样触发div
网站上有一个 div 框,并且有一个链接。我想单击容器以触发 :
<a href="#">sapien sed</a>
但是当我单击容器 div 时,它没有给出响应或者处于无限循环中。 我该如何解决这个问题? 也就是说;我想点击div触发
整个代码:
<div class="container">
<h2>Lorem ipsum dolor sit amet</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse congue molestie venenatis.
In adipiscing vulputate sem. Suspendisse potenti. Vestibulum nisl nibh, interdum sit amet pulvinar at,
malesuada vitae lacus. Aliquam semper <a href="#">sapien sed</a> risus laoreet ultrices. Donec volutpat felis eu justo
bibendum tempus. Donec non purus sed sapien fringilla tempor ut et metus. Nulla ipsum nibh, dapibus
ac ornare vitae, fermentum nec nulla.
</p>
</div>
<script type="text/javascript">
$(function(){
$("div.container").each(function(){
var $container = $(this);
$container.find("a:first").each(function(){
var $link = $(this);
$container
.css("cursor", "pointer")
.click(function(e){
$link.trigger("click");
return false;
});
$link.bind("click", function(e){
e.stopPropagation();
return true;
});
});
});
})
</script>
There is a div box on the web site and there is a link. I want to click to container in order to trigger <a>
:
<a href="#">sapien sed</a>
but When I clicked on the container div, it does not give response or it is in the infinite loop.
How can I solve this problem?
That is to say; I want to click the div to trigger <a>
The whole code:
<div class="container">
<h2>Lorem ipsum dolor sit amet</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse congue molestie venenatis.
In adipiscing vulputate sem. Suspendisse potenti. Vestibulum nisl nibh, interdum sit amet pulvinar at,
malesuada vitae lacus. Aliquam semper <a href="#">sapien sed</a> risus laoreet ultrices. Donec volutpat felis eu justo
bibendum tempus. Donec non purus sed sapien fringilla tempor ut et metus. Nulla ipsum nibh, dapibus
ac ornare vitae, fermentum nec nulla.
</p>
</div>
<script type="text/javascript">
$(function(){
$("div.container").each(function(){
var $container = $(this);
$container.find("a:first").each(function(){
var $link = $(this);
$container
.css("cursor", "pointer")
.click(function(e){
$link.trigger("click");
return false;
});
$link.bind("click", function(e){
e.stopPropagation();
return true;
});
});
});
})
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题是您期望
trigger("click")
导致链接的默认行为发生(即您期望链接被跟踪并加载它链接到的页面)。然而,这不是trigger
所做的。它所做的只是执行绑定到元素的click
事件处理函数。要实际点击该链接,您可以获取
href
并使用window.location
:这是一个 更新了小提琴。
The problem is that you're expecting
trigger("click")
to cause the default behaviour of the link to occur (i.e. you are expecting the link to be followed and the page it links to loaded). However, that is not whattrigger
does. All it does is execute theclick
event handler function bound to the element.To actually follow the link, you could grab the
href
and usewindow.location
:Here's an updated fiddle.
要同时触发标准“href”链接和带有 onClick 事件的链接:
To trigger standart "href" links and links with onClick event at the same time:
为了确保这一点,请将函数添加到内部,
否则 div 可能无法在代码运行之前加载。
to make sure, add the function inside of the
otherwise the div might not load before the code runs.