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/
4

2 に答える 2

5

わかりました、あなたが犯したエラー:

  • JsFiddle に jQuery を含めなかった
  • あなたの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/

于 2013-03-31T17:55:06.637 に答える
1

これを試して:

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

デモはこちら

于 2013-03-31T17:54:53.317 に答える