【问题标题】:jQuery .show/.hide/.toggle Not Working in Certain CircumstancesjQuery .show/.hide/.toggle 在某些情况下不起作用
【发布时间】:2012-03-24 02:34:22
【问题描述】:

我有 2 个 div 需要来回切换显示和不显示。这是我当前的 jQuery 代码

$(document).ready(function(){

    $('a:link').click(function(){
    $(".surveycontainer").toggle();
    $("#check").toggle();
    });

});

注意 #check 在通过 CSS 加载的页面时隐藏,因此该脚本表示,当单击链接时,每个链接都会被切换,从而使“surveycontainer”div 消失,“check”div 出现,反之亦然。

当在调查容器(包含链接)中调用 RSS 提要时,该脚本可以正常工作,并且在单击每个 div 时都会切换。将普通链接放置在 div 中时会出现问题,单击时“surveycontainer”被隐藏但“检查”未显示!

调查容器代码

<div class="surveycontainer" align="left">
<?php
// OFFERS

//Right here is a script that defines $country_code ("US" is what is called when the problem occurs)

<?php if($country_code=="US"): ?>
<a href="http://www.google.com" target="_blank">Yahoo</a>

<?php else: ?>
//Calls a RSS feed containing links here

</div>
<?php endif; ?>

如您所见,如果 $country_code 等于“US”,则会在新窗口中调用 Google 链接。

由于某些原因,当国家代码不等于 US 并且数据是从 RSS 提要中提取时,上面的 jQuery 脚本会在单击链接时切换每个 div。但是,当使用 Google 链接并单击时,只有“surveycontainer”div 被隐藏,“check”不显示。

我迫切需要解决这个问题!任何帮助都非常感谢,谢谢!

【问题讨论】:

    标签: jquery hide toggle show


    【解决方案1】:

    &lt;?php endif; ?&gt; 放在&lt;/div&gt; 之前,它不会结束你的surveycontainer div 当满足谷歌链接的条件时。所以重写你的代码看起来像:

    <div class="surveycontainer" align="left">
       <?php
       // OFFERS
       //Right here is a script that defines $country_code ("US" is what is called when the problem occurs)
       <?php if($country_code=="US"): ?>
          <a href="http://www.google.com" target="_blank">Yahoo</a>
       <?php else: ?>
           //Calls a RSS feed containing links here
       <?php endif; ?>
    </div>
    

    【讨论】:

    • 有效!我不敢相信这一切都是错的。太感谢了!我可以投票给你,因为最佳答案是 7 分钟,我一定会这样做。谢谢。
    • 我很乐意帮助你!如果您编写了格式化代码,您自己就会发现这个错误:)
    • @user1067458 - 请注意,如果您在使用输出 html 或 JS 的 PHP(或其他语言的服务器端代码)时遇到问题,有时只需使用“查看源代码”即可发现问题在浏览器中 - 在这种情况下,您会看到没有 &lt;/div&gt;...
    • 感谢您的提示 nnnnnn 一定会在发帖前使用它。
    猜你喜欢
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 2011-09-04
    • 1970-01-01
    相关资源
    最近更新 更多