[英]Trigger click on a tag when pressing fake read more button
Structure is as following: 结构如下:
div
h1
a
p
.more-info
When I click on .more-info div I need to simulate a click on the 'a'. 当我单击.more-info div时,我需要模拟对“ a”的单击。
$(".more-info").click(function() {
$(this).parent().find('h1').find('a').get(0).trigger('click');
})
This gives following error: 这给出了以下错误:
$(...).parent(...).find(...).find(...).get(...).trigger is not a function
Output of 输出
console.log($(this).parent().find('h1').find('a').get(0))
is the correct element I need to click. 是我需要单击的正确元素。
I've tried without .get(0) and get following when I log that 我试过没有.get(0)并在登录时得到关注
[a, prevObject: jQuery.fn.init(1), context: p.more-info, selector: "h1 a"]
.get(0)
returns the DOM element within the jQuery object, but .trigger()
is a jQuery function, so it needs to operate on the jQuery object. .get(0)
返回jQuery对象中的DOM元素,但是.trigger()
是jQuery函数,因此它需要在jQuery对象上进行操作。
But if you're trying to emulate the browser's built-in action when clicking on a link, .trigger()
won't do it. 但是,如果您在单击链接时尝试模拟浏览器的内置操作,则
.trigger()
不会这样做。 You need to call the DOM element's .click()
method. 您需要调用DOM元素的
.click()
方法。
$(".more-info").click(function() {
$(this).parent().find('h1').find('a').get(0).click();
});
See How to trigger a click on a link using jQuery 请参阅如何使用jQuery触发链接单击
You could use the element's .click() method, but I would recommend putting a link on the more-info element itself. 您可以使用元素的.click()方法,但我建议在more-info元素本身上放置一个链接。
Anyhow, this should work: 无论如何,这应该起作用:
$(this).parent().find('h1 > a').get(0).click();
Check out the fiddle here: https://jsfiddle.net/Lpyctuga/4/ 在这里查看小提琴: https : //jsfiddle.net/Lpyctuga/4/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.