如何在jQuery中的div中获取span标签并分配文本?

时间:2021-05-12 20:30:21

I use the following ,

我使用以下,

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

Now i want to find the span inside the div and assign a text to it...

现在我想在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 个解决方案

#1


47  

Try this:

尝试这个:

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

See it in your code!

在你的代码中看到它!

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");
}

#2


18  

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

or

要么

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

or

要么

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

or

要么

$("#message > a.close-notify").siblings('span').text("your text");

#3


4  

Try this

尝试这个

$("#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");
    });
}

#4


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");
    });
}

#1


47  

Try this:

尝试这个:

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

See it in your code!

在你的代码中看到它!

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");
}

#2


18  

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

or

要么

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

or

要么

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

or

要么

$("#message > a.close-notify").siblings('span').text("your text");

#3


4  

Try this

尝试这个

$("#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");
    });
}

#4


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");
    });
}