jquery切换文本只改变一次

发布于 2024-10-05 12:33:40 字数 1416 浏览 1 评论 0原文

我有以下代码,应该将 h2 的值从“显示更多”更改为“显示更少”,然后在单击父项时再次返回。

if() 测试正常并将文本更改为“显示更少”,但它永远不会再将其更改回来,并且始终显示警报“显示更多”,因此它只是没有检测到 else() 条件。

我已经四处寻找以找出原因,但尚未找到解决方案。

我知道这是一个菜鸟问题,所以对此表示歉意,但在发布到这里之前我已经尝试找到解决方案。

谢谢。

$('#panels > .feature').click(function() {
    if($(this).children('h2').val('Show More')) {
        $(this).children('h2').text('Show Less');
        alert('Show More was found');     
    }
    else {
        $(this).children('h2').text('Show More');
        alert('Show Less was found');
    }   
      $(this).next(".info_box").animate(
     {'height':'toggle'}, 'slow', 'linear'
     );    
});

有问题的 HTML 是:

<div id="panels">
 <div id="Email" class="feature">
    <h1>LiveContent</h1><h2>Show More</h2>
 </div>
<div class="info_box">Information will go in this div which should slide in and out.</div>
<div id="Email" class="feature">
  <h1>Publishing Solutions</h1><h2>Show More</h2>
 </div>
<div class="info_box">Information will go in this div which should slide in and out.</div> 
<div id="Email" class="feature">
  <h1>Title 1</h1><h2>Show More</h2>
 </div>
<div class="info_box">Information will go in this div which should slide in and out.</div>
</div>

I have the following code which should change the value of an h2 from "Show More" to "Show Less" and then back again when the parent is clicked.

The if() tests ok and changes the text to Show Less but it will never change it back again and always displays the alert "Show More" so it just isn't detecting the else() condition.

I have searched high and low to find out why but haven't found a solution yet.

I know this a noob question so apologise for this but I have tried finding the solution before posting here.

Thanks.

$('#panels > .feature').click(function() {
    if($(this).children('h2').val('Show More')) {
        $(this).children('h2').text('Show Less');
        alert('Show More was found');     
    }
    else {
        $(this).children('h2').text('Show More');
        alert('Show Less was found');
    }   
      $(this).next(".info_box").animate(
     {'height':'toggle'}, 'slow', 'linear'
     );    
});

The HTML in question is:

<div id="panels">
 <div id="Email" class="feature">
    <h1>LiveContent</h1><h2>Show More</h2>
 </div>
<div class="info_box">Information will go in this div which should slide in and out.</div>
<div id="Email" class="feature">
  <h1>Publishing Solutions</h1><h2>Show More</h2>
 </div>
<div class="info_box">Information will go in this div which should slide in and out.</div> 
<div id="Email" class="feature">
  <h1>Title 1</h1><h2>Show More</h2>
 </div>
<div class="info_box">Information will go in this div which should slide in and out.</div>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

难如初 2024-10-12 12:33:40
if($(this).children('h2').val('Show More')) {

这应该是 .text 而不是 .val 吗?

if($(this).children('h2').val('Show More')) {

Should this be .text instead of .val?

分分钟 2024-10-12 12:33:40

您正在使用 .val() 来检查文本,请使用 < a href="http://api.jquery.com/text/" rel="nofollow">.text() 代替(并比较结果,不要设置它),如下所示:

if($(this).children('h2').text() == 'Show More')) { //here
    $(this).children('h2').text('Show Less');
    alert('Show More was found');     
}
else {
    $(this).children('h2').text('Show More');
    alert('Show Less was found');
}

此外,您可以通过使用 < 的函数来整体精简它代码>.text().slideToggle() ,如下所示:

$('#panels > .feature').click(function() {
    $(this).children('h2').text(function(i, t) {
      return t == 'Show More' ? 'Show Less' : 'Show More';
    });
    $(this).next(".info_box").slideToggle("slow");
});

您可以在此处进行测试

You're using .val() to check the text, use .text() instead (and compare the result, don't set it), like this:

if($(this).children('h2').text() == 'Show More')) { //here
    $(this).children('h2').text('Show Less');
    alert('Show More was found');     
}
else {
    $(this).children('h2').text('Show More');
    alert('Show Less was found');
}

Also, you can slim it down overall by using a function for .text() and .slideToggle(), like this:

$('#panels > .feature').click(function() {
    $(this).children('h2').text(function(i, t) {
      return t == 'Show More' ? 'Show Less' : 'Show More';
    });
    $(this).next(".info_box").slideToggle("slow");
});

You can test it out here.

北座城市 2024-10-12 12:33:40

函数.val('Show More)尝试将HTML属性value设置为“Show More”。如果您不带参数调用它,它将充当getter,而不是setter - 这正是您想要的。但对于文本,而不是值。

因此,您需要的是 .text() == 'Show More' 之类的内容,而不是 .val('Show More')

The function .val('Show More) tries to set the HTML attribute value to 'Show More'. If you called it without parameters, it would act as a getter, not a setter - which is what you want. But for the text, not the value.

So instead of .val('Show More'), you want something like .text() == 'Show More'.

甜柠檬 2024-10-12 12:33:40

您正在设置文本而不是比较它。使用不带参数的 text 方法来获取值,然后进行比较:

if($(this).children('h2').text() == 'Show More')

即使是第一次,代码也无法正常工作,因为它总是做同样的事情。它第一次看起来有效的原因是它总是做第一次应该做的事情。

You are setting the text instead of comparing it. Use the text method without a parameter to get the value, and then compare it:

if($(this).children('h2').text() == 'Show More')

The code didn't work correctly even the first time, as it always did the same thing. The reason that it seemed to work the first time is that it happened to always do what it was supposed to do the first time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文