简体   繁体   English

如何获得jQuery.css()胜过样式表?

[英]How can I get jQuery.css() to trump a stylesheet?

I have been working on a page and I would like to programmatically set CSS properties on a cover slideshow according to how much screen real estate there is. 我一直在页面上工作,我想根据有多少屏幕空间以编程方式在封面幻灯片上设置CSS属性。 If the page is less than one boundary number, I don't want to activate the slideshow. 如果页面少于一个边界编号,则我不想激活幻灯片。 If the page has width greater than that boundary number, but less than another threshold, I want to have the slideshow shrink, and shrink more for smaller sizes. 如果页面的宽度大于该边界数,但小于另一个阈值,则我希望缩小幻灯片,而对于较小的尺寸,缩小更多。 Finally, if it is greater than both, I would like to horizontally center the slideshow in its logical container. 最后,如果大于两者,则希望将幻灯片放到逻辑容器中的水平位置。

The source at http://JonathansCorner.com/blacksmiths-forge/experimental.html has: http://JonathansCorner.com/blacksmiths-forge/experimental.html上的来源具有:

<iframe id="slideshow" border="0" frameborder="0" frameborder="no"
style="display: none;" width="318" height="424" src="/book_covers.cgi?width=318" />

The CSS at http://JonathansCorner.com/css/combined.css has: http://JonathansCorner.com/css/combined.css上的CSS具有:

#slideshow
    {
    height: 424px;
    position: fixed;
    top: 240px;
    right: 190px;
    width: 318px;
    }

And the JavaScript, at http://JonathansCorner.com/js/combined.js : 还有JavaScript,位于http://JonathansCorner.com/js/combined.js

if (jQuery(window).width() > 1200)
    {
    if (jQuery(window).width() > 1300)
        {
        var width = min(jQuery(window).width() - 1200, 318);
        document.getElementById('slideshow').src = "/book_covers.cgi?width=" +  width;
        document.getElementById('slideshow').width = width + 'px';
        document.getElementById('slideshow').width = width;
        jQuery('#slideshow').css('right', jQuery(window).width() - 1200 - 318);
        jQuery('#slideshow').css('display', 'block');
        }
    }

The mental model I walked in with was that jQuery.css() would be a trump card, and in the permutations I've tried so far, I've never been aware that a change has been made with jQuery.css. 我进入的思维模型是jQuery.css()将是一张王牌,到目前为止,在我尝试过的排列中,我从未意识到jQuery.css进行了更改。

I also tried using eg jQuery('#slideshow').css('display', 'block !important') . 我也尝试使用例如jQuery('#slideshow').css('display', 'block !important')

Is there any way I could use jQuery (or JavaScript alone) to set properties in a way that they will stick? 有什么方法可以使用jQuery(或仅JavaScript)来设置属性,使其保持不变?

(What is wrong with my initial understanding, and what is the truth about what can and can't be done with programmatic setting of DOM node styles and properties?) (我的最初理解是什么问题,关于通过编程设置DOM节点样式和属性可以做什么,不能做什么的真相是什么?)

You haven't closed your iframe tag, combined.js is never loaded. 您尚未关闭iframe广告代码,从未加载Combined.js。 iframe's aren't self-closing tags, they require an opening and a closing tag. iframe不是自闭标签,它们需要一个开始和结束标签。

<iframe></iframe>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM