单击具有特定 id 的 div 时,Javascript 重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17324356/
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
Javascript redirect when clicking on a div with a certain id
提问by SJCypher
Here is an example of what I need help with: http://www.arbutusroofing.com/roofing-services/
以下是我需要帮助的示例:http: //www.arbutusroofing.com/roofing-services/
When the user clicks on “READ MORE” (under each subject or category) and the light grey box pops up with more information, I want it to automatically redirect to another page in 10 seconds.
当用户点击“READ MORE”(在每个主题或类别下)并且弹出带有更多信息的浅灰色框时,我希望它在 10 秒内自动重定向到另一个页面。
I know how to redirect to a different page when an actual link is clicked, but not with certain events like this.
我知道如何在单击实际链接时重定向到不同的页面,但在某些事件中则不然。
Here is some example of the code:
这是代码的一些示例:
<h6 id="displayText" style="margin-top: 0px; cursor: pointer;"> <u>READ MORE</u> ABOUT FLAT ROOFING </h6>
<h6 id="displayText" style="margin-top: 0px; cursor: pointer;"> <u>阅读更多</u>关于平屋顶</h6>
I'm trying to redirect to a different page after 10 seconds when the id "displaytext" is clicked on.
单击 id“displaytext” 10 秒后,我试图重定向到不同的页面。
Also, here is the code for the toggle text if you were wondering:
此外,如果您想知道,这里是切换文本的代码:
<script type="text/javascript">
<script type="text/javascript">
$(document).ready(function() {
$("div#toggleText").hide();
$("h6#displayText").click(function(){
$(this).toggleClass("active");
$(this).next("div#toggleText").slideToggle();
});
});
</script>
</脚本>
Thanks.
谢谢。
回答by Fahad Ajmal
You JScode:
你的JS代码:
function OpenNewTab(id){
setTimeout(function(){ window.location.href = "http://yourlinkhere/'+id+'"; }, 10000);
}
HTML:
HTML:
<div onclick="OpenNewTab(1)">Readmore</div> // here 1 SHOULD BE UNIQUE ID AS PER RECORDS
回答by Neuticle
You're probably looking for the javascript function setTimeout.
您可能正在寻找 javascript 函数 setTimeout。
setTimeout(function(){ window.location.href = "http://somehwereblahblah"; }, 10000);
回答by Boundless
You can execute code with a delay using the setTimout function. An example of linking to google.com 10 seconds after the 'click' div is clicked:
您可以使用 setTimout 函数延迟执行代码。单击“点击”div 10 秒后链接到 google.com 的示例:
<div onclick='setTimeout(function(){window.location = "http://www.google.com"}, 10000)'>Click</div>
回答by Andrew L.
I think you can use settimeout function. Eg:
我认为您可以使用 settimeout 功能。例如:
setTimeout("location.href = 'http://insert-target-url';",1500);
setTimeout need two arguments. First is the action that will be executed. It can be simple redirect command, or event a function. The second one is the delay in milliseconds.
setTimeout 需要两个参数。首先是将要执行的操作。它可以是简单的重定向命令,也可以是事件函数。第二个是以毫秒为单位的延迟。
Hope this helps you.
希望这对你有帮助。