检查类和id是否存在,然后更改h1,然后在时间限制后重定向页面
check if class and id exist then change h1 then redirect page after time limit
我在HTML5页面中启动JS/JQ时遇到了一些问题。基本上,我想检查是否存在以下ID和以下类:
ID:page_blog
CLASS:页面当前
<section class="page current" id="page_blog" style="z-index: 99; left: -50px;">
...
...
...
如果它们存在,则在2-3秒后重定向,将H1更改为
正在加载。。。
<h1><button data-target="home" data-target-activation="click" class="back backwards"></button>Loading...</h1>
这就是我目前所拥有的:
<script type="text/JavaScript">
$(document).ready(function () {
$('li#blogLink.tile').click(function (e) {
e.preventDefault(); //will stop the link href to call the blog page
setTimeout(function () {
alert("this has worked");
//window.location.href = "http://www.site.co.uk/blog/"; //will redirect to your blog page (an ex: blog.html)
}, 2000); //will call the function after 2 secs.
});
});
</script>
我的页面上现在有以下内容:
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1.7");
</script>
<script type="text/JavaScript">
$(document).ready(function () {
if ($(".page.current").length) { // section exists
$("h1").text("Loading...");
setTimeout(function () {
window.location.href = "http://www.website.co.uk/blog/";
}, 2000);
});
</script>
但是检查类/id是否存在并没有启动?
试试这个:
$('li#blogLink.tile').click(function (e) {
e.preventDefault();
if ($("#page_blog.page.current").length) { // section exists
$("h1").text("Loading...");
setTimeout(function() {
window.location.href = "http://www.site.co.uk/blog/";
}, 2000);
}
});
小提琴示例
此外,值得一提的是,您的选择器过于挑剔。应该只有一个id为#page_blog
和#blogLink
的唯一元素,因此在它们的选择器上包含标记和类是多余的代码。
相关文章:
- Canvas+svg路径动画,在路径中的特定点暂停一段时间,然后继续
- 如何停止字幕文本一段时间,然后继续
- JS-移动图像,暂停设置的时间,然后将图像向后移动
- AngularJS:跟踪在Web应用程序中花费的时间,然后在特定时间触发事件
- JavaScript将人类可读的日期转换为unix时间戳,然后再转换回来
- Vanilla Js:在一个事件中开始一段时间的循环,然后用第二个事件打破它?('mouseover'启
- 淡入,然后使用css过渡淡出一段时间
- ng-hide 元素出现一段时间,然后隐藏
- 在页面首次加载时执行 jQuery 代码,然后在一段时间后加载
- 检测YouTube时间然后显示DIV?使用 JavaScript
- 在页面加载后淡出对象一段时间,然后在淡出后运行另一组代码
- 如何在页面加载时隐藏图像,然后在一段时间后显示滑动
- 如何让表单字段生成当前日期和时间,然后在提交时发送该信息
- 执行了一段时间的javascript/jquery函数,然后停止
- 如何使用Javascript按日期排序,然后按时间排序
- 节点 - MySQL 查询,需要几分钟时间,在几分钟后重复自己,然后完成原始查询
- jQuery:创建一个循环,首先需要一个时间间隔,然后更改为另一个时间间隔
- 检查类和id是否存在,然后更改h1,然后在时间限制后重定向页面
- 网页中的图像会出现一小段时间,然后消失
- 使用javascript,如何确定DIV可见的时间,然后将该时间长度记录到本地数据库中