简体   繁体   中英

How to get the closest element by finding innerhtml value using angular?

Am struggling to find the parent element by using value.

Here i have stored the whole html nodes in one variable.

$scope.mail_html = "<asd>My best friend is Mickey.</asd>Who is your favourite:<das class="sdfsdfsdf">Goofy</das>";

And my need is to find the closest element of the inner HTML value "Goofy";

The output should be <das class="sdfsdfsdf">;

Can anyone please help on this?

Using angular.element to put the html into a temporary div outside the dom and iterating through all the elements there.

Not really sure what objective is once found

 var mail_html = '<asd><span>My best friend is Mickey.</span></asd>Who is your favourite:<das class="sdfsdfsdf">Goofy</das>'; var $div = angular.element('<div>').append(mail_html), els = $div[0].querySelectorAll('*'), term = 'Goofy', target; for (var i = 0; i < els.length; i++) { if (els[i].textContent.indexOf(term) > -1) { target = els[i] } } if (target) { console.log( target) } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM