简体   繁体   中英

How can I insert a whole css class before an HTML element that I find?

I need to insert an image before an element I am trying to find in this code (also I am trying to add the class in the same function). The js:

insertSearchIcon: function(){
    $(document).find('jstree-icon').prepend('<div class="oob-dropdown">test</div>');
    }

And the css class I am trying to insert.

.oob-dropdown {
  background-image: url("/apps/cdpe/img/search_444444.png");
  background-color: transparent;
  background-repeat: no-repeat;
  height: 25px;
  width: 25px;
  border: none;
  padding-top: 1cm;
  position: relative;
}

Hopefully what I am trying to do is possible, but thanks for any help!

you probably missed the . in your selector find('jstree-icon') and secondly prepend() adds another item before the first child element of the matched selector. To add another element right before another you might be interested in before :

$('.jstree-icon').before('<div class="oob-dropdown">test</div>');

Btw: $(document).find() is probably not best practice, rather use the selector directly!

.prepend() inserts an element as the first child of another; it sounds like you need .before() . Your selector also needs a dot (assuming jstree-icon is a class).

$('.jstree-icon').before('<div class="oob-dropdown">test</div>');

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