2014-03-05 16 views
0

IAM新的JavaScript,我已經檢索使用getelement通過ID的一些數據,現在我想用一個特定部分getelementbytagname如何在Javascript中同時使用的getElementById和getElementByTagName

document.getElementById('titleUserReviewsTeaser').innerHTML; 
" 
     <h2>User Reviews</h2> 
     <div class="user-comments"> 
        <div class="tinystarbar" title="10/10"> 
         <div style="width: 100px;">&nbsp;</div> 
        </div> 
       <span itemprop="review" itemscope="" itemtype="http://schema.org/Review"> 
        <strong itemprop="name">Awesome review for an awesome movie</strong> 
        <span itemprop="reviewRating" itemscope="" itemtype="http://schema.org/Rating"> 
         <meta itemprop="worstRating" content="1"> 
         <meta itemprop="ratingValue" content="10"> 
         <meta itemprop="bestRating" content="10"> 
        </span> 
        <div class="comment-meta"> 
         25 August 2005 | by <a href="/user/ur6899565/?ref_=tt_urv"><span itemprop="author">Dragondrawer88</span></a> 
         <meta itemprop="datePublished" content="2005-08-25"> 
           (United States) 
         – <a href="/user/ur6899565/comments?ref_=tt_urv">See all my reviews</a> 
        </div> 
        <div> 
         <p itemprop="reviewBody">Balto has been a favorite movie of mine ever since it came out. This is the touching story of an out casted half dog half wolf named Balto voiced by the talented Kevin Bacon who's voice added a slight charm to the Balto character. The story takes place in Nome Alaska in the year 1925. A sickness as stricken the town's children and with out the antitoxin which is located hundreds of miles away in town of Nanana, the children will surly die. The dog team sent to retrieve the medicine which is led by Balto's almost arch nemesis Steel, is lost in a horrible snow storm. Now it is up to Balto to find the missing sled dog team and bring the medicine back to Nome before it is too late.<br><br>This movie is so gripping, I can never sit through the whole thing without balling my eyes out. This movie has a great plot which is based of a true story, a wonderful cast of voice actors, and a vary flowing animation style. This movie is so gripping and compelling that it as inspired me to become an cartoonist. I loved this movie so much that some how I found out I have three copies of it in my movie library. Surly this movie is worth seeing or owning if it makes a 17 year old shows tears for it, or sets the course for his career. Call me a geek but this movie is perfect for any one who likes talking animals, old school basic animation, and happy endings.</p> 
        </div> 
       </span> 
       <hr> 
       <div class="yn" id="ynd_1158238"> 
        17 of 20 people found this review helpful.&nbsp; 
        Was this review helpful to you? 
        <button class="btn small" value="Yes" name="ynb_1158238_yes" onclick="CS.TMD.user_review_vote(1158238, 'tt0112453', 'yes');">Yes</button> 
        <button class="btn small" value="No" name="ynb_1158238_no" onclick="CS.TMD.user_review_vote(1158238, 'tt0112453', 'no');">No</button> 
       </div> 
      <div class="see-more"> 

       <a href="/title/tt0112453/reviews-enter?ref_=tt_urv" rel="login" class="cboxElement">Review this title</a> 
       <span>|</span> 
        <a href="/title/tt0112453/reviews?ref_=tt_urv">See all 78 user reviews</a>&nbsp;» 
      </div> 
     </div> 
    " 

現在我想p itemprop之間的數據=「reviewBody」和/ p標籤我該如何做到這一點?

+0

你有什麼問題使用「getelementbytagname」?你有什麼嘗試?怎麼了? – Quentin

+0

我不知道如何同時使用它們兩個,我試過document.getElementById('titleUserReviewsTeaser')。innerHTML.getElementByTagName(「p」); 但即時獲取錯誤 –

+0

@ JP1016沒有方法'getElementByTagName';它是'getElementsByTagName' – Ian

回答

1
document.getElementById('titleUserReviewsTeaser').innerHTML.getElementByTagName("p"); 

首先,getElementByTagName不存在。多個元素可以具有相同的標籤名稱,所以函數是getElementsByTagName(複數!)並返回一個NodeList。

二,innerHTML是一個字符串。它不是一個DOM節點。您需要在DOM節點上調用getElementsByTagName

document.getElementById('titleUserReviewsTeaser').getElementsByTagName("p") 
+0

即時得到SyntaxError:意外的令牌ILLEGAL –

+0

@ JP1016:在'... byTagName(「p」)'行的末尾開始,並刪除最後10個字符左右。然後手動重新鍵入它們。在最後一個'('不應該在那裏的字符'後面有一個不可見的字符。 –

-1

試試這個:

alert(document.getElementsByTagName("p")[0].innerHTML); 
相關問題