2011-03-18 200 views
0

我需要的是找到具有類=「選擇」比分配該元素的一個變量內容的元素。查找和內容分配給一個變量

<a href="#">This is the content</a> 
<a href="#" class="selected">This is the content</a> 
<a href="#">This is the content</a> 

任何幫助?謝謝

+0

你應該閱讀有關jQuery的教程,學習基礎知識:HTTP://docs.jquery。 COM /教程:Getting_Started_with_jQuery文檔也很不錯:http://api.jquery.com/ – 2011-03-18 13:32:26

回答

2

使用class operator

var elementContents = $('.selected').html(); //assuming there is only one element found with the class selected

希望這有助於。

+0

@Achilles感謝您的編輯。 – Kevin 2011-03-18 13:18:55

0
var variable=$(".selected").text(); 

DEMO

1

變種somecontent寫入= $( '.selected')。 html();

+0

你的語法是錯誤的。 – SLaks 2011-03-18 13:15:12

+0

現在i'ts沒有錯...... – 2011-03-18 13:15:43

+0

我看。聰明... – SLaks 2011-03-18 13:19:18

0

給它一個ID,然後使用:

var content = document.getElementById("yourIdHere").innerHTML; 
0

,以確保您只得到第一次出現,不要忘記指示jQuery來只得到第一次,以確保它的可讀性我會使用爲.text()不是的.html()選擇,因爲它會剝奪任何HTML的路程,如果有任何

<a href="#">This is the content</a> 
<a href="#" class="selected">This is the content</a> 
<a href="#">This is the content</a> 

<script type="text/javascript"> 
$(document).ready(function() { 
    var myVariable = $(".selected").first().text(); 
}); 
</script>