2012-07-31 178 views
0

我試圖改變我的標題之一使用jQuery中的文本,但它只是將標題留空。我如何得到這個工作?更改標題文本

編輯:是應該改變航向

<!-- Logs --> 
    <div data-role="page" data-theme="a" id="page15"> 
     <div data-theme="a" data-role="header"> 
      <h3> 
       Logs 
      </h3> 
      <a data-role="button" data-direction="reverse" data-transition="slide" href="#page3" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left"> 
       Back 
      </a> 
     </div> 
     <div data-role="content" style="padding: 15px"> 
      <a data-role="button" data-transition="slide" href="#page1"> 
       Add log entry 
      </a> 
      <div data-role="collapsible-set" data-theme="" data-content-theme=""> 
       <div data-role="collapsible" data-collapsed="true"> 
        <h3> 
         July 2012 
        </h3> 
        <div data-role="collapsible-set" data-theme="" data-content-theme=""> 
         <div data-role="collapsible" data-collapsed="true" onclick="getLogTime(); this.onclick=null"> 
          <h3> 
           July 5 
          </h3> 
          <div data-role="collapsible-set" data-theme="" data-content-theme=""> 
           <div data-role="collapsible" data-collapsed="true" onclick="getLogData(); this.onclick=null"> 
            //Heading I'm trying to change 
            <h3 id=time1> 
            </h3> 
            <div> 
             <p id="logFortime1"> 
             </p> 
            </div> 
           </div> 
           <div data-role="collapsible" data-collapsed="true"> 
            <h3> 
             12:47 pm 
            </h3> 
           </div> 
          </div> 
         </div> 
         <div data-role="collapsible" data-collapsed="true"> 
          <h3> 
           July 6 
          </h3> 
         </div> 
        </div> 
       </div> 
       <div data-role="collapsible" data-collapsed="true"> 
        <h3> 
         August 2012 
        </h3> 
       </div> 
      </div> 
      <a data-role="button" data-transition="slide" href="#page21"> 
       Graphs 
      </a> 
     </div> 
    </div> 

功能:與標題

html頁面

function getLogTime() { 
$('#time1').text('Time') 
} 

編輯:

加了=所以在文本標題現在發生變化,但它會丟失它的所有樣式和可摺疊菜單中的內容。

+0

適合我的作品。 http://jsfiddle.net/landau/b9ggb/ – Trevor 2012-07-31 19:04:08

+0

你是否運行'$(document).ready(function(){...})內的代碼;'? – Trevor 2012-07-31 19:07:22

+0

@Trevor不,我不是 – Alwaysdeadfred 2012-07-31 19:22:47

回答

1

您應該使用標籤名稱或ID(首選)。不要組合多個選擇器(尤其是因爲您在任何時候都不應該在DOM中擁有多個ID)。 html()方法比text()方法快得多(請參閱http://jsperf.com/jq2-text-vs-html),但從語義上講,text()更合適。 確保DOM在執行任何代碼之前已加載(對於DOM)。以下是最快的,應該工作。

$(function(){ 
    $("#time1").html("Time"); 
}); 
0

爲什麼不使用純粹的jQuery解決方案?而不是在可點擊的元素上提供onclick attirbutes,你可以簡單地以這種方式使用jQuery。

HTML:

<h3 id="time1">Deepak</h3> 
<a id='changeheading'>click</a> 

的JavaScript/jQuery的:

$(document).ready(function(){ 
$('#changeheading').on("click",function(){  
$('#time1').text('Time'); 
}); 
});​ 

,它工作得很好。在這裏檢查演示http://jsfiddle.net/DeepakKamat/b9ggb/25/