2013-03-31 197 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你做