我有一些 jQuery 行,只有在满足条件要求时我才想应用它们。继承人的代码:
<script>
$(function() {
//<![CDATA[
if (location.pathname == "/one-page-checkout.asp")
$('#content_area').css("height", "1400px"); <!-- fix footer issue -->
$('#v65-onepage-ShippingCostDetails tr:nth-child(3)').css("display", "none");
//]]>
});
</script>
第一行,"$('#content_area').css("height", "1400px");"
,做我想做的,只有当 URL 有/one 时才适用-page-checkout.asp。但是第二行 $('#v65-onepage-ShippingCostDetails tr:nth-child(3)').css("display", "none");
普遍应用于我的网站.
我怎样才能让第二行和它后面的任何一行都遵守条件?
最佳答案
您的 if
语句中缺少大括号:
if (location.pathname == "/one-page-checkout.asp") {
$('#content_area').css("height", "1400px"); <!-- fix footer issue -->
$('#v65-onepage-ShippingCostDetails tr:nth-child(3)').css("display", "none");
}
不使用大括号是完全有效的,但是如果这样做,如果语句为真,则只会执行以下行。这可能导致维护期间出错。缩进只是为了便于阅读,对代码的执行没有任何影响。
您的代码按原样解释如下:
if (location.pathname == "/one-page-checkout.asp")
$('#content_area').css("height", "1400px"); <!-- fix footer issue -->
$('#v65-onepage-ShippingCostDetails tr:nth-child(3)').css("display", "none");
关于javascript - 在单个 JS 条件中应用多行 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17816879/