jQuery - 选择子div背景图像并修改它 [英] jQuery - Selecting a child div background image and amending it
问题描述
我正在寻找一种方法来改变div的背景图像,使用jQuery BUT只修改它,而不是完全改变它。
Im looking for a way to change the background image of a div using jQuery BUT only amending it, not totally changing it.
让我解释一下。
我正在使用 http://jqueryui.com/demos/ sortable / #portlets 显示一些打开和关闭的div。现在,当您单击它打开的portlet标题并关闭下面的内容时。
Im using http://jqueryui.com/demos/sortable/#portlets to show some div's that open and close. Now when you click the portlet header it opens and closes the content below.
在portlet标题内,我有一个子div,它显示一个箭头(向上或向下)取决于关于内容的当前状态。我需要一种方法来改变这个子div上的背景图像,方法是在背景图像的url末尾添加-visible。
Inside the portlet header i have a child div which shows an arrow (either up or down) depending on the current state of the content. I need a way of changing the background image on this child div by adding on "-visible" onto the end of the url for the background image.
我甚至都不知道从哪里开始这样做,但我在下面添加了一些代码供你查看。
I wouldnt even know where to start with doing this, but i have added some code below for you to look at.
从小提琴那里,我需要改变背景图像Portlet标头内的portlet-arrow div。我不能简单地一起更改背景图片,但我已将其简化为在此处发布。
From the fiddle there, i need to alter the background image of the portlet-arrow div inside portlet header. I can not simply change the background image all together, but i have simplified it down to post on here.
我希望这不是太窄,不能对任何人使用stackoverflow上的其他内容。
I hope this isnt too narrow to not be of use to anyone else on stackoverflow.
谢谢
推荐答案
也许我错过了这里的东西,但你不能使用 .css
属性修饰符为选定的jQuery对象?类似于:
Maybe I'm missing something here, but can't you use the .css
attribute modifier for the selected jQuery object? Something like:
var current_background = $("#my-div").css("background-image");
$("#my-div").css("background-image", current_background + "-visible");
如果您想要自己修改类名,可以尝试使用 .toggleClass()
, .hasClass()
, .addClass()
和 .removeClass()
jQuery中的方法。
If you're looking to modify the class names themselves, you can try mess around with the .toggleClass()
, .hasClass()
, .addClass()
and .removeClass()
methods in jQuery.
我希望这会有所帮助,但请告诉我,如果我'我完全错过了这个标记!
I hope this helps, but let me know if I've missed the mark here completely!
这篇关于jQuery - 选择子div背景图像并修改它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!