admin管理员组文章数量:1391951
Sorry if the question is worded weird but here is what I'm trying to do. I have to gather a specific line of info from a webpage. What I am having trouble with is I can not figure out the right selector to use, eq()
is not working . Here is the source from the page I'm trying to get the info from.
<div class="info" id="Data">
Domain Name: example
<br>
Registry: 12345
<br>
When I inspect element I get div#Data.info (text) I have tried $('div#info.Data').eq(1).text()
and a few other binations. I an new to scripting and the (text) part is what is I think is throwing me off.
Sorry if the question is worded weird but here is what I'm trying to do. I have to gather a specific line of info from a webpage. What I am having trouble with is I can not figure out the right selector to use, eq()
is not working . Here is the source from the page I'm trying to get the info from.
<div class="info" id="Data">
Domain Name: example.
<br>
Registry: 12345
<br>
When I inspect element I get div#Data.info (text) I have tried $('div#info.Data').eq(1).text()
and a few other binations. I an new to scripting and the (text) part is what is I think is throwing me off.
-
Are you able to return all of the text in that div? If so, you may want to use
.html()
instead of.text()
. This way you can then us.split()
with the<br>
s. You'd probably sayvar line_1 = $('div#info').html().split('<br>')[0]
– ntgCleaner Commented Nov 25, 2015 at 15:05 - To clarify, you want to retrieve the first line of an element using jQuery / JS? Is it possible you create paragraphs instead of separating them with line breaks? – Jonathan Lam Commented Nov 25, 2015 at 15:05
-
You may try like this,
var x= $("div#Data").text().split("\n")
.x
will contain array of lines from that div. – Vara Prasad Commented Nov 25, 2015 at 15:13 -
you have your id/classes reversed. it's
div#Data.info
, not the other way around. – Marc B Commented Nov 25, 2015 at 15:14 - If i use $('div#info.Data').text(); it returns the plete paragraph. I Had tried .split but it looks like i was using it incorrectly I tried .split('br'). What you said with the .html().split('<br>') worked. I'm just learning this and I appreciate the help. Thanks – Kevin h Commented Nov 25, 2015 at 15:20
2 Answers
Reset to default 9Let's look at your jQuery:
$('div#info.Data') // Gets <div> with id="info" and class="Data"
// ^ You have id and class reversed!
.eq(1) // This gets the 2nd element in the array
// ^ You only tried to get 1 element. What is the 2nd?
.text() // Returns bined text of selected elements
Also there's another issue. Your text is not in its own element. In order to get the textnodes in your current element, you could call .contents()
. jQuery does not treat text nodes like regular elements, though, so I would be careful doing any additional operations on them.
You can retrieve the text like such:
$("#Data").contents().eq(0).text() // -> 'Domain Name: example.'
$("#Data").contents().eq(2).text() // -> 'Registry: 12345'
Fiddle
First of all, you have you're id
and class
the wrong way round but this is a simple fix.
An alternative to your solution is to grab all the content, split it out into an array and then clean the empty strings caused by the new lines and <br />
tags.
This can then be used in any matter you like.
$(document).ready(function() {
var content = $('div#Data.info').eq(0).text();
var lines = content.split("\n").filter(Boolean)
console.log(lines);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="info" id="Data">
Domain Name: example.
<br>
Registry: 12345
<br>
</div>
本文标签: javascriptFind specific text in a paragraph by lineStack Overflow
版权声明:本文标题:javascript - Find specific text in a paragraph by line - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744584342a2614109.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论