我是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 :(得分:5)
好的,你犯的错误:
document.ready
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>
答案 1 :(得分:1)