31

私は以下を使用します、

<div id='message' style="display: none;">
  <span></span>
 <a href="#" class="close-notify">X</a>
</div>

今、私はdiv内のスパンを見つけて、それにテキストを割り当てたいと思います...

function Errormessage(txt) {
    $("#message").fadeIn("slow");
    // find the span inside the div and assign a text
    $("#message a.close-notify").click(function() {
        $("#message").fadeOut("slow");
    });
}
4

6 に答える 6

63

これを試して:

$("#message span").text("hello world!");

あなたのコードでそれを見てください!

function Errormessage(txt) {
    var m = $("#message");

    // set text before displaying message
    m.children("span").text(txt);

    // bind close listener
    m.children("a.close-notify").click(function(){
      m.fadeOut("slow");
    });

    // display message
    m.fadeIn("slow");
}
于 2010-04-20T06:38:14.060 に答える
22
$("#message > span").text("your text");

また

$("#message").find("span").text("your text");

また

$("span","#message").text("your text");

また

$("#message > a.close-notify").siblings('span').text("your text");
于 2010-04-20T06:42:24.843 に答える
4

これを試して

$("#message span").text("hello world!");

function Errormessage(txt) {
    var elem = $("#message");
    elem.fadeIn("slow");
    // find the span inside the div and assign a text
    elem.children("span").text("your text");

    elem.children("a.close-notify").click(function() {
        elem.fadeOut("slow");
    });
}
于 2010-04-20T06:40:48.933 に答える
0
function Errormessage(txt) {
    $("#message").fadeIn("slow");
    $("#message span:first").text(txt);
    // find the span inside the div and assign a text
    $("#message a.close-notify").click(function() {
        $("#message").fadeOut("slow");
    });
}
于 2011-11-09T13:32:13.160 に答える