这是基本的。
如何获得上述范围的值“这是我的名字”?
<div id='item1'>
<span>This is my name</span>
</div>
我认为这应该是一个简单的例子:
$('#item1 span').text();
要么
$('#item1 span').html();
$("#item1 span").text();
假设您打算让它读取id =“ item1”,则需要
$('#item1 span').text()
$('#item1').text(); or $('#item1').html();
适用于 id="item1"
由于您没有为“ item”值提供属性,因此我假设正在使用一个类:
<div class='item1'>
<span>This is my name</span>
</div>
alert($(".item span").text());
确保您等待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>
在javascript中,您不使用document.getElementById('item1').innertext
吗?
$('span id').text(); worked with me
$('#id span').text()
就是答案!
$('#item1 span').html();
它与我的代码一起工作
非常重要有关.text()和.html()之间区别的其他信息:
如果您选择选择一个以上的项目,如您有两个跨度像现在这样
<span class="foo">bar1</span>
<span class="foo">bar2</span>
,
然后
$('.foo').text();
附上这两个文本,然后给你;而
$('.foo').html();
仅给您其中之一。
您可以在HTML中直接在span中使用id。
<span id="span_id">Client</span>
然后你的jQuery代码将是
$("#span_id").text();
有人帮助我检查错误,发现他使用val()而不是text(),不可能在span中使用val()函数。所以
$("#span_id").val();
将返回null。
<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>
本文地址:http://javascript.askforanswer.com/ruheshiyongjqueryhuoqudezhi.html
文章标签:html , javascript , jquery
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
文章标签:html , javascript , jquery
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!