【问题标题】:Jquery toggle background?Jquery切换背景?
【发布时间】:2015-12-22 22:19:13
【问题描述】:
星期

丹麦克朗

" id="radio2" value="75" />

我有一堆由while循环生成的div条目,所以我想实现这个:当点击条目内的输入时,我想要改变条目div的背景图像,但是如果我在另一个条目div中选择另一个收音机..我'希望所有以前的更改(例如以前的背景更改)都消失并更改当前 div 的背景..这是我尝试做的也许你可以提供帮助..这是下面的 jquery

$("input").click(function() {

             $(this).parent("div").toggle(function() {
             $(this).parent("div").css("background", "url(images/div_bg_hover.png)");
             }, function() {
             $(this).parent("div").css("background", "url(images/div_bg.png)");

             });

有些东西出了故障..我的意思是代码 inst' 工作在所有..有人可以帮助我吗?谢谢


这两个我都试过了:

$("input").click(function() {
                 $("input").each(function() {
                     if (this.checked) {
                         $(this).parent("div").addClass("highlight");
                     }
                     else {
                         $(this).parent("div").removeClass("highlight");
                     }
                 });
             });

$("input").click(function() {
                 $("input").each(function() {
                     if (this.checked) {
                         $(this).parent("div").css("background", "url(images/div_bg_hover.png)");
                     }
                     else {
                         $(this).parent("div").css("background", "url(images/div_bg.png)");
                     }
                 });
             });

由于某种原因,它根本不工作,没有改变背景,也许它必须对上面的代码做一些事情,这里是完整的脚本:

 <script type="text/javascript">

         $(document).ready(function() {

             $(".left_navigation a:last").toggleClass('bolder');


             $("input").each(function() {
                 var element_value = $(this).val();
                 $(this).prev("p").append(" " + element_value + ",-");
             });


             $("input").click(function() {
                 $("input").each(function() {
                     if (this.checked) {
                         $(this).parent("div").addClass("highlight");
                     }
                     else {
                         $(this).parent("div").removeClass("highlight");
                     }
                 });
             });




         });

</script>

【问题讨论】:

    标签: jquery user-interface hover radio-button


    【解决方案1】:

    我看到的第一个直接问题是,您的 jQuery 在其末尾缺少 });,除非您忘记将其粘贴到那里。除了抛出错误之外,这将阻止它做任何事情......

    这是您之前尝试过的简化版本。它至少可以使调试稍微容易一些。

    <script type="text/javascript">
    $(document).ready(function(){
        $("input").click( function() { 
             $("input").parent("div").removeClass("highlight");
             $(this).parent("div").addClass("highlight");
          });
      });
    </script>
    

    虽然,我认为您之前的尝试没有任何明显错误,这让我想知道$("input") 是否与您认为的相符。我可能至少会提醒您单击功能中的某些内容以进行完整性检查。实际上,我发现有几次我的$(document).ready 在我认为之前就被触发了,也就是说,在我的输入准备好之前,因为我的框架......

    【讨论】:

    • 没有缺少});,背景是一个完全有效的快捷方式
    • 缺少第一组javascript });可能不是问题。只是没有粘贴。 Eduardo 对背景的看法是正确的,它是一个有效的 css 速记,所以我把它编辑掉了……
    【解决方案2】:

    有几个问题:

    • 您正在使用Toggle 并且切换功能是:

    如果它们被显示,切换使它们 隐藏(使用隐藏方法)。如果 它们是隐藏的,切换使它们 显示(使用 show 方法)。

    您应该询问是否检查了收音机

    • 您应该调用 each 函数,以更改其他单选 div 值

    • 你应该调用$(document).ready()函数

    示例(警告:Javascript 不是我最好的武器):

    <script>
    $(document).ready(function(){
        $("input").click( function() { 
             $("input").each(function() { 
                 if (this.checked) {
                    $(this).parent("div").css("background-color", "green") }
                 else {
                    $(this).parent("div").css("background-color", "red")}
              });
          });
      });
    </script>
    

    注意:为了更好的测试,我使用了背景颜色,但你对背景的使用很好。

    【讨论】:

      【解决方案3】:

      您可能需要使用 'background-image' 而不仅仅是 'background' 来设置图像。

      编辑:以为我之前在 jQuery 中使用“背景”css 属性时遇到过问题,但刚刚在各种浏览器上对其进行了测试,看来我要么弄错了,要么自从我使用它以来它已在 jQuery 版本中得到修复。

      【讨论】:

      • 这不是问题。背景是一个完全有效的捷径
      猜你喜欢
      • 2011-10-04
      • 2010-10-19
      • 1970-01-01
      • 1970-01-01
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多