简体   繁体   English

单击灯箱上的关闭按钮时,重定向到另一个 url

[英]On clicking close button on a lightbox redirect to another url

Here is the thing.事情就是这样。 I'm working on elementor EA lightbox.我正在研究 elementor EA 灯箱。 So when I click on an image a lightbox with video pops up.因此,当我单击图像时,会弹出一个带有视频的灯箱。 So I want it to redirect when the person clicks on the "X" button.所以我希望它在用户点击“X”按钮时重定向。 We can achieve it with a script by adding an ID to the button and on click redirect to a link, I know.我知道,我们可以通过向按钮添加 ID 并单击重定向到链接来使用脚本来实现它。 But the problem is with elementor, we can't actually add ID or Class to the close button.但问题在于 elementor,我们实际上无法将 ID 或 Class 添加到关闭按钮。 This is how the close button looks like.这就是关闭按钮的样子。

<button title="Close" type="button" class="mfp-close">×</button>

This is the code I tried.这是我尝试过的代码。 But it is not working with "getElementsByClassName" for some reason.但由于某种原因,它不能与“getElementsByClassName”一起使用。 And as I said I even can't add an ID to the button.正如我所说,我什至无法为按钮添加 ID。

<script type="text/javascript">
    document.getElementsByClassName("mfp-close").onclick = function () {
        location.href = "www.google.com";
    };
</script>

Can anyone see how this could work?任何人都可以看到这是如何工作的吗? Any help please?请问有什么帮助吗?

TRY using querySelector instead of getElementByClassName尝试使用querySelector而不是getElementByClassName

document.querySelector(".mfp-close").onclick = function () {
    location.href = "www.google.com";
};

document.getElementsByClassName("mfp-close") return an array document.getElementsByClassName("mfp-close")返回一个数组

you can try你可以试试

document.getElementsByClassName("mfp-close")[0] document.getElementsByClassName("mfp-close")[0]

<script type="text/javascript">
    document.getElementsByClassName("mfp-close")[0].onclick = function () {
        location.href = "www.google.com";
    };
</script>

Change class to ID and then:将 class 更改为 ID,然后:

<script type="text/javascript">
    $("#mfp-close).click(function () {
    window.location.href = "www.google.com";
    });
</script>

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

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