2014-12-05 143 views
1

我有一個問題:如何更改jss內容的css屬性:url在jquery中?

<div id="arrow"></div> 
<div id="p0" style="content: url(images/cerchioSelezionato.png)"></div> 
<div id="p1" style="content: url(images/cerchio.png)"></div> 

我的目標是:當我#arrow元素上點擊,我想那#P0的圖像的Cerchio成爲反之亦然爲了這個,我在jQuery的寫了這個代碼:

$(document).ready(function(){ 
    $("#arrow").click(function(){ 
     $("#p1").attr({content:url(images/cerchioSelezionato.png)}); 
     $("#p0").attr({content:url(images/cerchio.png)}); 
    } 
} 

當我在鉻箭頭單擊控制檯給我這個錯誤:

Uncaught ReferenceError: url is not defined 
slider_script.js:12(anonymous function)  slider_script.js:12m.event.dispatch jquery-1.11.1.min.js:3r.handle 
+0

的值必須是現貨感謝引用字符串 – charlietfl 2014-12-05 14:54:47

回答

3

的Try ...

$(document).ready(function(){ 
    $("#arrow").click(function(){ 
     $("#p1").attr({style: "content:url(images/cerchioSelezionato.png)" }); 
     $("#p0").attr({style: "content:url(images/cerchio.png)" }); 
    } 
} 

你改變Style屬性,但內容屬性。

3

使用.css(),而不是.attr()操縱日e風格。 另外,我想你忘了引用。

$(document).ready(function(){ 
    $("#arrow").click(function(){ 
     $("#p1").css({content:'url(images/cerchioSelezionato.png)'}); 
     $("#p0").css({content:'url(images/cerchio.png)'}); 
    }); 
}); 

雖然鍵不必在JavaScript對象中引用,值必須是。

由於您只更改content,因此可以使用語法.css('content', ...)代替對象notaion。我保持它的方式,所以你可以在將來添加規則。

+0

@Rhumborl :) – 2014-12-05 15:01:09

0

你可以試試這個也

$(document).ready(function(){ 
    $("#arrow").click(function(){ 
     $("#p1").css('content','url(images/cerchioSelezionato.png)'); 
     $("#p0").css('content','url(images/cerchio.png)'); 
    } 
}