2013-03-31 193 views
0

我不熟悉javascript/jquery,有一些基本的東西可以忽略。我想通過調用一個函數來替換一些文本的內容。絕對我嘗試過的每件事都失敗了。用內容替換Div

http://jsfiddle.net/spuder/dTGBy/

HTML

<body> 
<div id=test> 

</div> 
</body> 

腳本

$(document).ready(function() { 

    getTest(); 

} 



function getTest() { 
    $("#test").html("Hello World"); 
    //$('#test').replaceWith('<h2>New heading</h2>'); 
    //$("#test").attr("text","http://www.w3schools.com/jquery"); 
    //$("#test").attr("href","http://www.w3schools.com/jquery"); 
    //$("#test").html("New text"); 
    //$(".test").html("New text"); 
    //$("test")html.("New text"); 
    //$("test").update("New text"); 
    //document.getElementById("testSpan").innerHTML = "42"; 
    // $("#test").html("<b>Hello world!</b>"); 
} 

//This is supposed to be super easy^

//http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_dom_html_set 
//http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_dom_attr_set 
//http://www.willmaster.com/library/web-development/replace-div-content.php 
//http://api.jquery.com/replaceWith/ 
+0

有缺少一個右括號完成你準備好()調用 - 你在控制檯的Javascript錯誤看? –

+1

爲什麼每行之前都有兩個斜槓! – shnisaka

+1

您鏈接到的jsfiddle不包含jQuery庫,並且您使用的$表達式需要jQuery – Marryat

回答

5

好,誤差:

  • 不包括jQuery的中的jsfiddle
  • 錯誤地結束了您的document.ready
  • 忘記周圍的DIV ID報價

JS:

$(document).ready(function(){ 
    getTest(); 
}); 



function getTest() { 
    $('#test').replaceWith('<h2>New heading</h2>'); 
    //$("#test").attr("text","http://www.w3schools.com/jquery"); 
    //$("#test").attr("href","http://www.w3schools.com/jquery"); 
    //$("#test").html("New text"); 
    //$(".test").html("New text"); 
    //$("test")html.("New text"); 
    //$("test").update("New text"); 
    //document.getElementById("testSpan").innerHTML = "42"; 
    // $("#test").html("<b>Hello world!</b>"); 
} 

HTML:

<div id="test"> 

</div> 

小提琴http://jsfiddle.net/ajp36/1/

+1

快速,快速,100%準確..你值得+1 – shnisaka

+0

謝謝@shnisaka :) –

1

試試這個:

$(document).ready(function() { 
    getTest(); 
}); 

DEMO HERE

0你做