简体   繁体   English

使用 javascript jquery 在元素之后或之前添加 html

[英]Add html after or before an element using javascript jquery

I need to add html immediately after the li or immediately before the href in the following HTML:我需要在以下 HTML 的 li 之后或 href 之前立即添加 html:

<li class="current-cat cat-parent" data-toggle="collapse" role="button" aria-expanded="false" aria-controls="collapseMenu" href="#childcats" id="parent-sidebar">
  <a href="http://www.mydomain/">Visit</a>
</li>

This is what I need to achieve:这是我需要实现的:

<li class="current-cat cat-parent" data-toggle="collapse" role="button" aria-expanded="false" aria-controls="collapseMenu" href="#childcats" id="parent-sidebar">
  <i class="fa"></i>
  <a href="http://www.mydomain/">Visit</a>
</li>

I tried this but it makes it part of the a and doesn't work:我试过这个,但它使它成为a一部分并且不起作用:

jQuery('li.cat-parent a').prepend('<i class="fa"></i>');

Is this possible?这可能吗?

Given the HTML structure you're looking to create you need to append to the a , not the li :鉴于您要创建的 HTML 结构,您需要 append 到a ,而不是li

jQuery('li.cat-parent').prepend('<i class="fa"></i>');

prepend() adds the new element inside and at the start of the target element. prepend()在目标元素的内部和开头添加新元素。

before() will add it as a sibling, that is the one you should use here. before()会将其添加为同级,这是您应该在此处使用的。

Here are some reference links:以下是一些参考链接:

https://api.jquery.com/before/ https://api.jquery.com/before/

https://api.jquery.com/after/ https://api.jquery.com/after/

https://api.jquery.com/prepend/ https://api.jquery.com/prepend/

https://api.jquery.com/append/ https://api.jquery.com/append/

You need to modify your selector to the .cat-parent element since that's the one you're adding to.您需要将选择器修改为.cat-parent元素,因为这是您要添加的元素。 Also make sure the i element has the font awesome class correct to properly show the icon.还要确保i元素的字体 awesome class 正确以正确显示图标。

jQuery('li.cat-parent').prepend('<i class="fa fa-">test</i>');

There are 6 solutions to solve this question.有6个解决方案可以解决这个问题。 2 of them are in vanilla js.其中 2 个在香草 js 中。 Here are 4 of them:以下是其中的 4 个:

Insert in parent as first element (jQuery)插入父元素作为第一个元素(jQuery)

prependTo() prependTo()

Insert every element in the set of matched elements to the beginning of the target.将匹配元素集中的每个元素插入到目标的开头。

More info: jQuery API Docs - prependTo()更多信息: jQuery API 文档 - prependTo()

 const setup = () => { /** Insert in parent as first element */ //jquery const $catParent = $('.cat-parent'); $('<i class="fa"></i>').prependTo($catParent); } window.addEventListener('load', setup);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <li class="current-cat cat-parent" data-toggle="collapse" role="button" aria-expanded="false" aria-controls="collapseMenu" href="#childcats" id="parent-sidebar"> <a href="http://www.mydomain/">Visit</a> </li>

Insert in parent as first element (Vanilla JS)插入父元素作为第一个元素(Vanilla JS)

The insertAdjacentHTML() method of the Element interface parses the specified text as HTML or XML and inserts the resulting nodes into the DOM tree at a specified position. Element 接口的insertAdjacentHTML()方法将指定的文本解析为 HTML 或 XML 并将生成的节点插入到指定 position 的 DOM 树中。

'afterbegin' : Just inside the ´element`, before its first child. 'afterbegin' :就在 'element' 内部,在它的第一个子元素之前。

More info: MDN Web Docs - insertAdjacentHTML()更多信息: MDN Web 文档 - insertAdjacentHTML()

 const setup = () => { /** Insert in parent as first element */ //vanilla js const catParent = document.querySelector('.cat-parent'); catParent.insertAdjacentHTML('afterbegin', '<i class="fa"></i>'); } window.addEventListener('load', setup);
 <li class="current-cat cat-parent" data-toggle="collapse" role="button" aria-expanded="false" aria-controls="collapseMenu" href="#childcats" id="parent-sidebar"> <a href="http://www.mydomain/">Visit</a> </li>

Insert before link (jQuery)在链接之前插入(jQuery)

insertBefore()插入前()

Insert every element in the set of matched elements before the target.在目标之前插入匹配元素集中的每个元素。

More info: jQuery API Docs - insertBefore()更多信息: jQuery API 文档 - insertBefore()

 const setup = () => { /** Insert before link */ //jquery const $catParentLink = $('.cat-parent > a'); $('<i class="fa"></i>').insertBefore($catParentLink); } window.addEventListener('load', setup);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <li class="current-cat cat-parent" data-toggle="collapse" role="button" aria-expanded="false" aria-controls="collapseMenu" href="#childcats" id="parent-sidebar"> <a href="http://www.mydomain/">Visit</a> </li>

Insert before link (Vanilla JS)在链接之前插入(Vanilla JS)

The insertAdjacentHTML() method of the Element interface parses the specified text as HTML or XML and inserts the resulting nodes into the DOM tree at a specified position. Element 接口的insertAdjacentHTML()方法将指定的文本解析为 HTML 或 XML 并将生成的节点插入到指定 position 的 DOM 树中。

'beforebegin' : Before the element itself. 'beforebegin' :在element本身之前。

More info: MDN Web Docs - insertAdjacentHTML()更多信息: MDN Web 文档 - insertAdjacentHTML()

 const setup = () => { /** Insert before link */ //vanilla js const catParentLink = document.querySelector('.cat-parent > a'); catParentLink.insertAdjacentHTML('beforebegin', '<i class="fa"></i>'); } window.addEventListener('load', setup);
 <li class="current-cat cat-parent" data-toggle="collapse" role="button" aria-expanded="false" aria-controls="collapseMenu" href="#childcats" id="parent-sidebar"> <a href="http://www.mydomain/">Visit</a> </li>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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