简体   繁体   English

如何触发锚标记通过单击另一个锚标记驻留在页面上

[英]how to trigger an anchor tag resides on the page by clicking on another anchor tag

I have model where i have two links click and click1 the pop is behind click and i want to trigger the popup when user clicks on click1 how can i do that . 我有模型,我有两个链接点击并单击1弹出后面点击,我想在用户点击click1时触发弹出窗口我该怎么做。 The anchor tags are here. 锚标签在这里。

 <a href="#modal">Click</a>
    <a href="#modal1">Click1</a>

And the script i have written for it. 我为它编写的脚本。

 <script type="text/javascript">
    $("#model1").click(function(){
     $("#model").trigger("click");
        });
    </script>

Change it to the code below, if you like to leave your HTML code: 如果您想保留HTML代码,请将其更改为以下代码:

<script type="text/javascript">
$("a[href=#modal1]").click(function(){
    $("a[href=#modal]").trigger("click");
});
</script>

Or change your HTML to the code below, if you like to leave your JS code: 或者,如果您想保留JS代码,请将HTML更改为以下代码:

<a href="#modal" id="modal">Click</a>
<a href="#modal1" id="modal1">Click1</a>

The second variation has much better performance. 第二种变化具有更好的性能。

Not entirely sure what you're trying to do, but I'll give it a shot anyway: 不完全确定你想要做什么,但无论如何我都会试一试:

Say you have the following markup: 假设您有以下标记:

<a href="#" id="modal1">Modal 1</a>
<a href="#" id="modal2">Modal 2</a>

Now as far as I understood you want to trigger the click event on the second anchor by clicking the first. 现在据我所知,你想通过点击第一个来触发第二个锚点上的点击事件。 Here it is: 这里是:

$("#modal1").on("click", function(e) {
    $("#modal2").click();
    e.preventDefault();
});

$("#modal2").on("click", function(e) {
    alert("MODAL 2 CLICKED");
});

Here is a fiddle trying to demonstrate it. 这是一个试图证明它的小提琴

In case you're looking for a way to redirect to Modal2 by clicking on Modal1 just by invoking the click event wont work. 如果你正在寻找一种通过点击Modal1重定向到Modal2的方法,只需通过调用click事件就行不通。 If that's the case you need to look for a different approach, perhaps reading the href attribute and redirecting the user to the url. 如果是这种情况,您需要寻找不同的方法,也许阅读href属性并将用户重定向到URL。

Ok its so simple assign an id to second link like that. 好吧,这么简单就像这样给第二个链接分配一个id。

<a href="#modal">Click</a>
<a href="#" id=modal1>Click1</a>

and write the javascript for it. 并为它写javascript。

<script type="text/javascript">

    $("#modal2").on("click", function(e) {
    location.href = "#modal";
    e.preventDefault();
});
</script>

Because as far as I am seeing that the popup is loading after first link href so why we don't redirect this second click to this first link href where the popup resides and I hope it will work. 因为据我所知,弹出窗口是在第一个链接href之后加载的,所以为什么我们不将这个第二次点击重定向到弹出窗口所在的第一个链接href,我希望它能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM