2013-01-19 43 views
0

div標籤中特定的標籤我有一個div標籤:更改使用jQuery

<div id="content"><p></p><a href="#"></a></div> 

我要動態地不同的內容插入到<p>標籤,並進入<a>標籤的HREF屬性。

我正在使用jQuery。

+0

jQuery的API文檔是相當廣泛的。您應該查看一下:http://api.jquery.com/。 jQuery教程也可能會有幫助:http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery。 –

+0

部分副本http://stackoverflow.com/q/179713/218196 –

回答

4

一樣簡單:

$("#content > p").html("Some content"); 
$("#content > a").prop("href", "/new/link.html"); 

還是在鏈:

$("#content") 
    .find("p").html("Some content") 
    .end() 
     .find("a").prop("href", "/new/link.html"); 
+0

謝謝!如果我有2個標籤,例如我該如何更改其中的一個? – user550413

+1

@ user550413您[class selectors](http://api.jquery.com/class-selector/):'$(「#content a.blue」)。prop(...);'。 – VisioN

+0

我試過了,但是因爲我的類在它的名稱中有空格(我在css中使用它),它不起作用。 – user550413