Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

javascript - How do I get a class as a string and also the innerHTML from an element within S.fn.init?

S.fn.init?[div.definitionAgelenidae, prevObject: S.fn.init(558)]

I have tried:

var Test02 = $(".definition").children().filter(function () {
    return $(this).css("visibility") == "visible";
};

console.log(Test02[0]);

I would like to get the string definitionAgelenidae

question from:https://stackoverflow.com/questions/65598514/how-do-i-get-a-class-as-a-string-and-also-the-innerhtml-from-an-element-within-s

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Directly accessing $.fn.init is not a good idea, and should be avoided. Given the goal you describe in the comments (copied below) there is an alternative.

I would like to get the innerhtml of the visible element so I can have a more specific search result

In this case you can supply another condition in the function you provide to filter() which checks for the presence of a specific word. Something like this:

let term = 'dolor';

let $matches = $(".definition").children().filter(function() {
    return $(this).is(":visible") && $(this).text().toLowerCase().includes(term.toLowerCase());
}).addClass('match');
.match { color: #C00; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="definition">
  <p>Lorem ipsum dolor sit</p>  
</div>
<div class="definition">
  <p>Ipsum dolor sit amet</p>  
</div>
<div class="definition">
  <p>Foo bar fizz buzz</p>  
</div>
<div class="definition">
  <p>Dolor sit amet consectetur</p>  
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...