如何使用jQuery获得的值?

时间:2022-11-27 07:58:25

This is basic.

这是基本的。

How do I get the value 'This is my name' of the above span?

如何获得上面span的值'This is my name' ?

<div id='item1'>
<span>This is my name</span>
</div>

10 个解决方案

#1


258  

I think this should be a simple example:

我认为这应该是一个简单的例子:

$('#item1 span').text();

or

$('#item1 span').html();

#2


11  

$("#item1 span").text();

#3


5  

Assuming you intended it to read id="item1", you need

假设您希望它读取id=“item1”,那么您需要它

$('#item1 span').text()

#4


5  

$('#item1').text(); or $('#item1').html(); works fine for id="item1"

$(' # item1 ')。text();或$(' # item1 '). html();对id =“item1”很不错

#5


5  

Since you did not provide an attribute for the 'item' value, I am assuming a class is being used:

由于您没有为“项目”值提供属性,所以我假设正在使用一个类:

<div class='item1'>
  <span>This is my name</span>
</div>

alert($(".item span").text());

Make sure you wait for the DOM to load to use your code, in jQuery you use the ready() function for that:

确保您等待DOM加载使用您的代码,在jQuery中您使用ready()函数:

<html>
 <head>
  <title>jQuery test</title>
  <!-- script that inserts jquery goes here -->
  <script type='text/javascript'>
    $(document).ready(function() { alert($(".item span").text()); });
  </script>
</head>
<body>
 <div class='item1'>
   <span>This is my name</span>
 </div>
</body>

#6


2  

In javascript wouldn't you use getelementbyid('item1').innertext?

在javascript中,你不会使用getelementbyid('item1').innertext吗?

#7


1  

     $('span id').text(); worked with me

#8


1  

$('#id span').text() is the answer!

$('#id span').text()是答案!

#9


0  

$('#item1 span').html(); Its working with my code

$(" # item1跨度”). html();它和我的代码一起工作

#10


-2  

<script>
    $(document).ready(function () {

            $.each($(".classBalence").find("span"), function () {
            if ($(this).text() >1) {
                $(this).css("color", "green")

            }
            if ($(this).text() < 1) {
                $(this).css("color", "red")
                $(this).css("font-weight", "bold")
            }
        });

    });
    </script>

#1


258  

I think this should be a simple example:

我认为这应该是一个简单的例子:

$('#item1 span').text();

or

$('#item1 span').html();

#2


11  

$("#item1 span").text();

#3


5  

Assuming you intended it to read id="item1", you need

假设您希望它读取id=“item1”,那么您需要它

$('#item1 span').text()

#4


5  

$('#item1').text(); or $('#item1').html(); works fine for id="item1"

$(' # item1 ')。text();或$(' # item1 '). html();对id =“item1”很不错

#5


5  

Since you did not provide an attribute for the 'item' value, I am assuming a class is being used:

由于您没有为“项目”值提供属性,所以我假设正在使用一个类:

<div class='item1'>
  <span>This is my name</span>
</div>

alert($(".item span").text());

Make sure you wait for the DOM to load to use your code, in jQuery you use the ready() function for that:

确保您等待DOM加载使用您的代码,在jQuery中您使用ready()函数:

<html>
 <head>
  <title>jQuery test</title>
  <!-- script that inserts jquery goes here -->
  <script type='text/javascript'>
    $(document).ready(function() { alert($(".item span").text()); });
  </script>
</head>
<body>
 <div class='item1'>
   <span>This is my name</span>
 </div>
</body>

#6


2  

In javascript wouldn't you use getelementbyid('item1').innertext?

在javascript中,你不会使用getelementbyid('item1').innertext吗?

#7


1  

     $('span id').text(); worked with me

#8


1  

$('#id span').text() is the answer!

$('#id span').text()是答案!

#9


0  

$('#item1 span').html(); Its working with my code

$(" # item1跨度”). html();它和我的代码一起工作

#10


-2  

<script>
    $(document).ready(function () {

            $.each($(".classBalence").find("span"), function () {
            if ($(this).text() >1) {
                $(this).css("color", "green")

            }
            if ($(this).text() < 1) {
                $(this).css("color", "red")
                $(this).css("font-weight", "bold")
            }
        });

    });
    </script>