简体   繁体   中英

Find element in document using HTML DOM

I need to be able to select and modify an element in an HTML document. The usual way to find an element using jQuery is by using a selector that selects by attribute, id, class or element type. However in my case I have the element's HTML DOM and I want to find the element on my document that matches this DOM.

Important :

I know I can use a class selector or ID selector etc.. but sometimes the HTMLs I get don't have a class or an ID or an attribute to select with, So I need to be able to select from the element's HTML.

For example here is the element I need to find :

<span class='hello' data='na'>Element</span>

I tried to use jQuery's Find() but it does not work, here is the jsfiddle of the trial : https://jsfiddle.net/ndn9jtbj/

Trial :

el = jQuery("<span class='hello' data='na'>Element</span>");
jQuery("body").find(el).html("modified element");

The following code does not make any change on the element that is present in my HTML and that corresponds to the DOM I have supplied. Is there any way to get the desired result either using native Javascript or jQuery?

You could filter it by outerHTML property if you are sure how browser had parsed it:

var $el = jQuery("body *").filter(function(){
   return this.outerHTML === '<span class="hello" data="na">Element</span>';
});
$el.html("modified element");
el = jQuery('<i class="fa fa-camera"></i>');

This does not say "find the element that looks like <i class="fa fa-camera"></i> ". It means "create a new i element with the two classes fa and fa-camera . It's the signature for creating new elements on the fly .

jQuery selectors look like CSS, not like HTML. To find the i element with those two classes, you need a selector like i.fa.fa-camera .

Furthermore $("document") looks for an HTML element called document . This does not exist. To select the actual document, you need $(document) . You could do this:

$(document).find('i.fa.fa-camera').html("modified html")

or, more simply, you could do this:

$('i.fa.fa-camera').html('modified html');

You indicate in a comment to your question that you need to find an element based on a string of HTML that you receive. This is, to put it mildly, difficult, because, essentially, HTML ceases to exist once a browser has parsed it. It gets turned into a DOM structure. It can't just be a string search.

The best you can do is something like this:

var searchEl = jQuery('<i class="fa fa-camera"></i>');
var tagName = searchEl.prop('tagName');
var classes = [].slice.apply(searchEl.prop('classList'));

$(tagName + "." + classes.join('.')).html('modified html');

Note that this will only use the tag name and class names to find the element. If you also want IDs or something else, you'd need to add that along the same lines.

You should use Javascript getting the elements by something like

document.getElementById...
document.getElementsByClassName...
document.getElementsByTagName...

Javascript is returning the elements with the Id, Class or Tag Name you chose.

You can get en element with document.querySelector('.fa-camera')

with querySelector you can select IDs and Classes

You can simply refer to it by its class names.

$('.fa.fa-camera').html("modified html");

Similar to this answer https://stackoverflow.com/a/1041352/409556

Here is a full example:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $('.fa.fa-camera').html("modified html");

});

</script>
</head>
<body>
<i class="fa fa-camera"><h1>Some HTML</h1></i>


</body>
</html>`

The one thing that you could use is to check attributes (class and id goes here too in some way) that element have, and the build jQuery selector or DOM querySelector to find the element you need. The hardest part would be to find element based on innerHTML property - "Element" text inside it, for this one you'll probably have to grab all similar element and then search through them.

<span class='hello' data='na'>Element</span>
jQuery('body').find('span.hello[data=\'na\']').html('modified element')

Take notice of 'span' - that's tag selector, '.hello' - class, '[data="na"]' data attribute with name of data.

Jsfiddle link here that extends your example;

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