简体   繁体   English

我应该如何从js的多个范围中获取“这是我的名字”?

[英]How should I get 'This is my name ' from multiple spans in js?

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

Need JS code to fetch a specific span 需要JS代码来获取特定范围

You can use: 您可以使用:

document.getElementById("item1").children[0];

To get the first child of your div (ie the first span) and then use .textContent on this node to get "This is my name" 要获取div的第一个孩子(即第一个跨度),然后在此节点上使用.textContent来获取“这是我的名字”

See working example below: 请参见下面的工作示例:

 const firstChild = document.getElementById("item1").children[0]; console.log(firstChild.textContent); 
 <div id='item1'> <span>This is my name</span> <span> This is my nickname</span> </div> 

Or, if you wish to use jQuery: 或者,如果您想使用jQuery:

 const firstChild = $("#item1").children().eq(0); console.log($(firstChild).text()); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id='item1'> <span>This is my name</span> <span> This is my nickname</span> </div> 

  1. Use selector :contains() to select the span which contains the text This is my name 使用选择器:contains()选择包含文本的跨度This is my name

 $("span:contains(This is my name)").css('color', 'red') 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <span>This is my name</span> <span> This is my nickname</span> 

You can use find with eq like below. 您可以将findeq一起使用,如下所示。

 var item1 = $('#item1').find('span').eq(0).text(); var item2 = $('#item1').find('span').eq(1).text(); console.log(item1); console.log(item2); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id='item1'> <span>This is my name</span> <span> This is my nickname</span> </div> 

Using jQuery you can use 使用jQuery,您可以使用

$('#item1 > span:first-child').text()

 var text = $('#item1 > span:first-child').text(); console.log(text); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id='item1'> <span>This is my name</span> <span> This is my nickname</span> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从多个跨度中检索具有相同类名的文本 - Retrieve Text from multiple spans with the same class name 如何使用jQuery在HTML页面中创建所有跨度 - How do I get all spans created in my HTML page using jQuery 如何获取具有特定类名的字符串变量中的所有跨度 - How to get all spans in string variable, with particular class name 我应该如何定义我的构造函数(JS) - How should I define my constructor (JS) 如何使用跨度中的值填充隐藏文本字段以访问这些值? - How do I get populate hidden text fields with values from spans in order to access the values? 如何使用javascript从多个连续范围中获取单个组合字符串 - How to get a single combined string out of multiple consecutive spans with javascript 如何获取多个范围的html内容并将值添加为类 - How to get html contents of multiple spans and add the value as class 如何使用 onMouseHover 更改我的跨度 state? - How can i change my spans state using onMouseHover? 我如何并且应该从另一个文件加载我的.js文件以保持整洁? - How can I, and should I, load my .js files from a different file for cleanliness? 我应该如何命名我的javascript库函数,以便它们可以与普通的旧javascript区分开来? - How should I name my javascript library functions so they are differentiable from plain old javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM