【发布时间】:2016-05-22 19:10:47
【问题描述】:
谁能告诉我如何为这个给定的javascript添加或分配更多属性。
$('#mydoom').each(function () {
if ($(this).css('visibility') == 'hidden') {
document.location.href = "http://www.example.com";
}
});
你看到上面的脚本只有一个css标签/属性visibility:hidden,我怎样才能添加更多的属性...
类似这样的示例:
$('#mydoom').each(function () {
if ($(this).css('visibility') == 'hidden')
($(this).css('display') == 'none')
($(this).css('font-size') == '25px')
($(this).css('color') == 'red')
{
document.location.href = "http://www.example.com";
}
});
您看到上面的脚本包含许多属性...那么我该怎么做,这不起作用,我在上面作为示例向您展示了。
我更新了帖子以使其更加清晰:
这里的例子是我的页面:它有这个脚本:
我创建是为了帮助传达,如何使这个脚本工作。
<script type='text/javascript'>
//<![CDATA[
$(document).ready(function() {
$('#mydoom').each(function () {
if
($(this).css('font-size') != '25px') // See if the font-size is **25** if not then redirect.
($(this).css('color') != 'red') // See if the color is **red** if not redirect .
($(this).css('position') != 'relative') // See if the position is **relative** if not redirect.
($(this).css('float') != 'left') // See if the float is **left** if not redirect.
{
document.location.href = "http://www.example.com";
}
});
})
//]]>
</script>
我也添加了 html 和错误的 css 属性来检查重定向
<style>
#mydoom {position:absolute;}
#mydoom {font-size:24px;}
#mydoom {color:white;}
</style>
<div id="mydoom">
mydoom
</div>
你看,我放了 position:absolute font-size:24px 和 color:white 但是页面没有重定向,为什么?因为我已经在 javascript 中设置来检查它的值,如果它不匹配然后重定向页面。那么如何使脚本工作。
我希望你明白了。
【问题讨论】:
-
你想做什么? css 属性处于“IF”状态。您可以使用逻辑运算符将它们链接起来。
-
@maniexx 我想要一个重定向,如果 if 语句检查分配给 id #mydoom 的几个 css 属性...然后重定向页面...它使用单个属性但如何添加到检查多个标签,如颜色、字体大小和我在那里设置的许多标签......谢谢
-
@RajeshSurry 如果您要检查每个属性是否都是这样,请将每个检查与
&&连接起来。 -
@RajeshSurry 而且,
#mydoom只能有一个,因为它是一个id。 -
@RajeshSurry 请告诉我我的理解是否正确?
标签: javascript jquery html css redirect