简体   繁体   English

如何仅在第一页上应用此样式?

[英]How can I apply this style only on the first page?

I have this site: 我有这个网站:

link 链接

CODE JS: 代码JS:

$('.entry-content').css('height', 100+'vh');
$('.entry-content').css('height', $('.entry-content').outerHeight() - 50);
$(window).on('resize',function(){

    $('.entry-content').css('height', 100+'vh');
    $('.entry-content').css('height', $('.entry-content').outerHeight() - 50);

});

These functions should be applied only on the first page. 这些功能应仅应用于第一页。 Can I access different div style to apply only on the first page? 我可以访问不同的div样式以仅应用于第一页吗?

I tried to put this code, but unfortunately does not work ... apply on all pages. 我试图把这个代码,但不幸的是不起作用...适用于所有页面。

CODE CSS: 代码CSS:

article>.entry-content{height:auto !important;}

Can you help me solve this problem? 你能帮我解决这个问题吗?

Thanks in advance! 提前致谢!

EDIT: 编辑:

 if ( window.currentPage == "homepage" ) {

    $('.entry-content').css('height', 100+'vh');
    $('.entry-content').css('height', $('.entry-content').outerHeight() - 50);
    $(window).on('resize',function(){

    $('.entry-content').css('height', 100+'vh');
    $('.entry-content').css('height', $('.entry-content').outerHeight() - 50);

    });
 }

Code work in document.ready code only go in ... if you put in resize function not working. 在document.ready代码中的代码工作只进入...如果你把调整大小功能不起作用。

Bad way, 糟糕的方式,

You can check specific DOM element length which only added on homepage 您可以检查仅在主页上添加的特定DOM元素长度

// #onlyhomepage Only exist DOM on homepage
if ( $("#onlyhomepage").length ) {

    $('.entry-content').css('height', 100+'vh');
    $('.entry-content').css('height', $('.entry-content').outerHeight() - 50);
    $(window).on('resize',function(){

        $('.entry-content').css('height', 100+'vh');
        $('.entry-content').css('height', $('.entry-content').outerHeight() - 50);

    });

}

Good way, 好办法,

Simply add a global JavaScript variable on your homepage 只需在您的主页上添加一个全局JavaScript变量即可

HTML ( Homepage HTML ) HTML(主页HTML)

<script>
    window.currentpage = "homepage";
</script>

JavaScript ( External Js File ) JavaScript(外部Js文件)

if ( window.currentPage == "homepage" ) {

    $('.entry-content').css('height', 100+'vh');
    $('.entry-content').css('height', $('.entry-content').outerHeight() - 50);
    $(window).on('resize',function(){

        $('.entry-content').css('height', 100+'vh');
        $('.entry-content').css('height', $('.entry-content').outerHeight() - 50);

    });

}

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

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