点击一个元素触发点击另一个元素[JQuery]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21662734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:39:22  来源:igfitidea点击:

Click on an element triggers the click on another element [JQuery]

jqueryonclickclick

提问by Fabio

I have a top menu with the buttons home, prodotti, infoand contatti. Below there is an accordion menu that has always an open slide and when you click on a title it changes and opens the relative slide.

我与按钮的顶部菜单homeprodottiinfocontatti。下面是一个手风琴菜单,它总是有一个打开的幻灯片,当您单击标题时,它会更改并打开相关幻灯片。

screenshot

截屏

I would like that when I press for example on contattiin the top menu it triggers the click on contattaciin the accordion menu so that the relative slide opens as if i clicked directly on contattaci(using JQuery).

我希望当我contatti在顶部菜单中按下例如时,它会触发contattaci手风琴菜单中的点击,以便相关幻灯片打开,就像我直接点击一样contattaci(使用 JQuery)。

screenshot2

截图2

The HTML of the top menu looks like this:

顶部菜单的 HTML 如下所示:

<div id='cssmenu'>
                    <ul>
                        <li id="home" class='active'><span>Home</span></li>

                        <li id="prodotti"><span>Prodotti</span></li>

                        <li id="info"><span>Info</span></li>

                        <li id="contact" class='last'><span>Contatti</span></li>
                    </ul>
                </div>

The HTML of the accordion menu looks like this:

手风琴菜单的 HTML 如下所示:

<div class="container">
<div id="demo">
<ol>
...................
<li id="contattaci">
<h2><span>Contattaci</span></h2>
<div>
<iframe src="form.php"width="625px" height="400px" ></iframe>
</div>
<p class="ap-caption">e-mail</p>
</li>
......
</ol>
</div>
<div>

So when I click on the contattiitem of the top menu (with id="contact") I would like to simulate a click on "contattaci" in the accordion menu (with id="contattaci") so that a slide with a contact form opens next to "contattaci".

所以,当我在点击contatti顶部菜单项(使用id =“ contact‘),我想模拟一个点击’contattaci手风琴菜单‘(使用id =’ contattaci”),以便与联系人形式的幻灯片旁边打开“ contattaci”。

I'm not sure that it is possible, anyway I tried using this JQuery code:

我不确定这是否可能,无论如何我尝试使用此 JQuery 代码:

<script>
    $(document).on('click', '#contact', function() {
        $("#contattaci").click();
    });
</script>

This doesn't work. I hope you can help me to fix the problem.

这不起作用。我希望你能帮我解决这个问题。

EDIT: The css accordion menu uses css3 but also a jquery library called jquery.accordionpro.min.js that makes use of JQuery Swipe library (https://github.com/jgarber623/jquery-swipe). Probably the slide can be opened using a function in Jquery Swipe library. What do you think? If you can help let me know.

编辑:css 手风琴菜单使用 css3,但还有一个名为 jquery.accordionpro.min.js 的 jquery 库,它利用了 JQuery Swipe 库 ( https://github.com/jgarber623/jquery-swipe)。可能可以使用 Jquery Swipe 库中的函数打开幻灯片。你怎么认为?如果你能帮忙告诉我。

Linkto the website for further analysis: http://tinyurl.com/m2u5t7d

链接到网站进行进一步分析:http: //tinyurl.com/m2u5t7d

回答by pschueller

I would try it this way:

我会这样尝试:

$(document).on('click', '#contact', function(event) { 
    event.preventDefault(); 
    $("#contattaci h2").click(); 
});

回答by Pramod Kharade

$(document).on('click', '#contact', function(e) {
    $("#contattaci").trigger('click');
});

回答by kwyjibear

I know I'm a little late to the party but...For the inner click

我知道我参加聚会有点晚了但是......对于内部点击

$("#contattaci h2").click();

change it to

将其更改为

$("#contattaci h2").trigger("click");