1

我有一个 jQuery 函数,通常可以在 Volusion 页面上运行,但由于某种原因现在它不起作用。location.pathname.indexOf 以具有该 URL 的所有页面为目标(站点使用 GET 变量在 SearchResults.asp 页面上进行搜索)。我已经将引用从单打改为双打,我似乎想不出其他任何东西来测试它。有人在这段代码中看到任何语法错误吗?不应该有任何冲突,因为它只运行 jQuery(没有其他类似 MooTools 的东西)。我还尝试在 document.ready 之后发出“测试”警报,但屏幕上没有任何反应。谢谢!

<script>
$(document).ready(function() {  
    if (location.pathname.indexOf('/SearchResults.asp') != -1 ) {
        $('div#content_area').css({'display','none !important'});         
    }
});
</script>
4

2 回答 2

8

您有语法错误。

这个:

$('div#content_area').css({'display', 'none !important'});

应该是这样的:

$('div#content_area').css({'display': 'none !important'});
//                                  ^
//                                  |
//                                  | look here

使用.css()时,您可以使用 2 种变体。

您可以使用它来更新单个属性,该属性使用,分隔 CSS 属性的名称和值,类似于:

$('div#content_area').css('display', 'none !important');

或者您可以使用 jQuery 1.9 中添加的新变体,它允许您一次指定多个样式,从而允许您指定属性值对,类似于:

$('div#content_area').css({
    'display': 'none !important',
    'border' : 'solid 1px red'
});

css() 和 !important


.css()尝试使用and应用样式时似乎存在问题!important。有一个很久以前提出的错误:Ticket #2066已关闭,并在勾选中显示了替代方案。

它提到,作为替代方案,您可以cssText在使用多样式变体时设置与此类似的内容:

$('div#content_area').css({
    'cssText': 'display: none !important'
});

或者在使用单一样式变体时:

$('div#content_area').css('cssText', 'display: none !important');

不过,正如勾选的提到的那样,请注意:

您必须小心设置cssText,因为它会设置/清除该元素的 css 中的所有内容。

考虑到 的副作用,另一种选择很可能是最安全的cssText,它是创建一个单独的 CSS 类并应用它,类似于:

.alwaysHide{
    display: none !important;
}
$('div#content_area').addClass('alwaysHide');

希望这可以帮助。

于 2013-04-10T20:30:30.293 回答
4

您正在尝试使用 2 种语法样式。

要么,您需要这样做:

<script>
    $(document).ready(function() {  
        if (location.pathname.indexOf('/SearchResults.asp') != -1 ) {
            $('div#content_area').css('display','none !important');         
        }
    });
    </script>

或者你需要使用这个:

<script>
    $(document).ready(function() {  
        if (location.pathname.indexOf('/SearchResults.asp') != -1 ) {
            $('div#content_area').css({'display' : 'none !important'});         
        }
    });
    </script>
于 2013-04-10T20:35:48.000 回答