简体   繁体   English

一种JavaScript / jQuery函数在Wordpress中不起作用

[英]One JavaScript/jQuery Function Not Working in Wordpress

So, I'm trying to use Wordpress for the "News" page on my website, but one of my js functions is just not working in Wordpress (works fine without wp). 因此,我试图在我网站的“新闻”页面上使用Wordpress,但是我的js函数之一在Wordpress中不起作用(没有wp即可正常工作)。

Here is the HTML I've put in the Wordpress posts template: 这是我放入Wordpress帖子模板中的HTML:

<div class="posts">
    <h2>POST SUMMARY</h2>
    <a class="readmore" href="javascript:;">Read More</a>
    <div class="border"></div>
    <p>POST CONTENT</p> 
    <center><iframe>iFRAME CONTENT</iframe></center>
    <a class="readless" href="javascript:;">Read Less</a>
</div>

These functions are working fine: 这些功能运行良好:

// Showing only post preview on page load
$('.posts').not(':first').css({
    'height': '110px',
    'overflow': 'hidden'
});

// Hiding a.readmore when post is expanded
$('.readmore:first').addClass('full');

// Expanding post
$('.readmore').click(function(){
    $(this).addClass('full');
    $(this).closest('.posts').css({
        'height': '',
        'overflow': ''
    });
});

This is the problem function ( .readless collapses the post as it should, but .readmore remains hidden): 这是问题函数( .readless折叠帖子,但.readmore.readmore隐藏状态):

// Minimizing post
$('.readless').click(function(){
    var clicked = $(this);
    clicked.closest('.posts').css({'height': '110px','overflow': 'hidden'});
    clicked.closest('.posts').children('.readmore').removeClass('full');
});

Oh, and the CSS, in case it's not obvious: 哦,还有CSS,以防不明显:

a.readmore.full {
    display: none;
}

My only thought was that .posts already exists in wp's HTML, so I tried giving it a name attribute and targeting it (and its children) that way, but to no avail. 我唯一的想法是.posts已经存在于wp的HTML中,因此我尝试给它提供了一个name属性,并以此为目标(及其子级),但无济于事。

Any help and/or suggestion is, as always, very much appreciated. 一如既往,非常感谢您的任何帮助和/或建议。

You can troubleshoot Javascript by using the console in your browser. 您可以使用浏览器中的控制台对Javascript进行故障排除。 For example, you can try typing these commands to the console: 例如,您可以尝试在控制台中键入以下命令:

console.log( jQuery('.readless') );
console.log( jQuery('.readless').closest('.posts') );
console.log( jQuery('.readless').closest('.posts').children('.readmore') );

The output will tell you if jQuery is matching your elements correctly. 输出将告诉您jQuery是否正确匹配您的元素。

Edit : Turns out the OP was posting the code through the Rich Editor, and the A elements got wrapped with P elements. 编辑 :原来OP正在通过Rich Editor发布代码,并且A元素被P元素包裹。 In this case, the a.readmore element was no longer a child of div.posts , but a descendant (a grandchild). 在这种情况下, a.readmore元素不再是一个孩子 div.posts ,但后代 (孙子)。 Therefore, the jQuery selector is: 因此,jQuery选择器是:

jQuery('.readless').closest('.posts').find('.readmore');

If you have difficulty matching the correct elements, always check your DOM using the browser's developer tools such as "inspect element". 如果您难以匹配正确的元素,请始终使用浏览器的开发人员工具(例如“检查元素”)检查DOM。

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

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