Jquery切换背景?

发布于 2024-08-04 11:26:33 字数 2317 浏览 3 评论 0原文

week

DKK

" 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)");

             });

出现故障.. 我的意思是代码安装'在工作..有人能帮助我吗?谢谢,


我已经尝试过这两种方法:

$("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>

week

DKK

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

I have bunch of these div entry generated by while loop so I want to achieve this : WHEN input inside entry is clicked I want background image of entry div to change, but if I choose another radio in another entry div .. I'd like all previous changes( such as previous change of background) to go away and to change background of current div .. here is what I tried to do maybe you can help .. here is jquery below

$("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)");

             });

Something is malfunctioning .. I mean code inst' working at all .. can anyone help me ? thank you


I've tried both this :

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

AND

$("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)");
                     }
                 });
             });

for some reason its not working at all not changing the background , maybe it has to do something with the code above here is the complete script :

 <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>

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

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

发布评论

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

评论(3

红焚 2024-08-11 11:26:33

我看到的第一个直接问题是您的 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 在我认为是在我的输入准备好之前就被触发了,因为我的框架......

The first immediate problem I see is that your jQuery is missing a }); at the end of it unless you just forgot to paste it there. That would stop it from doing anything other than throwing an error...

This is a simplified version of you've tried before. It might at least make debuging slightly easier.

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

Although, I don't see anything obviously wrong about what you tried before which makes me wonder if $("input") is matching what you think it is. I would probably at least alert some things inside your click function for sanity checking. I've actually found a few times that my $(document).ready was firing before I thought it was i.e. before my inputs were ready because of my framework...

何处潇湘 2024-08-11 11:26:33

有几个问题:

  • 您正在使用 Toggle 并且 Toggle 函数是:

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

您应该询问是否选中了无线电

  • 您应该调用each函数,以便更改其他radio 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>

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

There are several problems:

  • You are using Toggle and Toggle function is:

If they are shown, toggle makes them
hidden (using the hide method). If
they are hidden, toggle makes them
shown (using the show method).

You should ask if the radio is checked

  • You should call the each function, in order to change the other radio div values

  • You should call the $(document).ready() function

Sample (Warning: Javascript is not my best weapon):

<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>

Note: I've used backgound-color for better testing, but your usage of background is good.

吃→可爱长大的 2024-08-11 11:26:33

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

编辑:我以为我之前在 jQuery 中的“background”css 属性上遇到了麻烦,但是刚刚在各种浏览器上测试了它,看来我要么是错误的,要么自从我使用它以来它在 jQuery 版本中得到了修复。

you might need to use 'background-image' instead of just 'background' to set the images.

edit: thought I'd had trouble with the 'background' css property in jQuery before, but having just tested it on various browsers it seems I was either mistaken or it was fixed in jQuery versions since I worked with it.

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