简体   繁体   English

JavaScript运行完整的A href命令

[英]JavaScript Run full A href command

I have a website where there is a side menu filled with links. 我有一个网站,其中有一个包含链接的侧面菜单。 On top of that are some Next and Prev buttons for the user to switch between the menus of links. 最重要的是一些“下一步”和“上一步”按钮,供用户在链接菜单之间切换。

I want to change this so that the menu will automatically change after x amount of time. 我想更改此设置,以便菜单将在x时间后自动更改。

I thought something like this would do it: 我认为这样可以做到:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
 function delayer(){
 window.location = "http://www.google.com" }
 </script>
</head>

<body  onLoad="setTimeout('delayer()', 1000)">
</body>

</html>

Basically, instead of opening google, I want the page to run the "Next" button which is represented by: 基本上,我不想打开google,而是希望页面运行“下一步”按钮,该按钮由以下内容表示:

<div class="navBtns mar9 s3">

<a href="#" class="prev"><span></span></a>
<a href="#" class="next"><span></span></a>

</div>

Any idea on how to do this? 关于如何执行此操作的任何想法? Thanks!! 谢谢!!

<a href="#" class="prev"><span></span></a>

This hyperlink does nothing by itself. 此超链接本身不执行任何操作。 Somewhere on the site, there is a javascript function bound to the click event of this link. 网站上的某个地方,有一个JavaScript函数绑定到此链接的click事件。 You need to either trigger a click event on the link, or call the javascript directly. 您需要在链接上触发click事件,或直接调用javascript。

Without seeing the rest of the javascript / knowing what frameworks are in use on the page, it's impossible to give a more precise answer. 如果不查看javascript的其余部分/不知道页面上正在使用什么框架,就不可能给出更精确的答案。

-- EDIT -- -编辑-

Based on your comment, you may be able to do something along these lines: 根据您的评论,您可以按照以下方式进行操作:

<script type="text/javascript">
    setTimeout(function() {
        $('#page_HOME .slider .next').click();
    }, 1000);
</script>

As long as those hyperlinks are contained inside the slider element, the above code will trigger a change in your side menu after 1000 milliseconds 只要那些超链接包含在slider元素内,上面的代码将在1000毫秒后触发边菜单中的更改

If you'd like to "click" the next button, then you can do that programatically with JS. 如果您想“单击”下一个按钮,则可以使用JS进行编程。

var nextbutton = document.getElementsByClassName('next');
nextbutton.click();

Getting element by class like that only works on post-IE8 browsers. 像这样按类获取元素仅适用于IE8后的浏览器。

You could find the HREF of the link you want based on the class name of the link, using plain old JS. 您可以使用普通的旧JS根据链接的类名称找到所需链接的HREF。

window.location = document.querySelector(<link class name>).getAttribute("href");

This will redirect the browser to whatever the href attribute is set to. 这会将浏览器重定向到href属性设置为的任何位置。

If you wanted to keep a function like you have, you could use this: 如果您想保持像您一样的功能,可以使用以下方法:

function delay(link, time) {
    setTimeout(function() {
        window.location = document.querySelector("." + linkClass).getAttribute("href");
    }, time);
}

Then to use it, just say: 然后使用它,只需说:

delay("next", 5000); // go to the href of the link with the class "next" after 5 seconds.

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

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