简体   繁体   English

jQuery-在选定的div中添加html内容(如插件)

[英]jquery - Add html content within a selected div like a plugin

I would like to add some html content within a selected div like most of the plugins are. 我想像大多数插件一样在选定的div中添加一些html内容。

Eg: 例如:

<div id="selected-div">
</div>

$('#selected-div').foo();

When the div ( selected-div ) is passed to the function, I would like to add the below html content inside that div ( selected-div ). 当div( selected-div )传递给函数时,我想在该div( selected-div )中添加以下html内容。

<div id="lorem-ipsum-wrapper">
  <ul id="lorem-ipsum-main">
    <li id="btn-header-wrapper" class="main-btn">
      <a href="#" id="btn-T">T</a>
      <ul>
        <li>
          <a href="#" id="btn-T1">T1</a>
        </li>
        <li>
          <a href="#" id="btn-T2">T2</a>
        </li>
        <li>
          <a href="#" id="btn-T3">T3</a>
        </li>
        <li>
          <a href="#" id="btn-T4">T4</a>
        </li>
      </ul>
    </li>
    <li class="main-btn">
      <a href="#" id="btn-E"><b>E</b></a>
    </li>
    <li id="btn-justify-wrapper" class="main-btn">
      <a href="#" id="btn-gender">Gender</a>
      <ul>
        <li>
          <a href="#" id="btn-M">M</a>
        </li>
        <li>
          <a href="#" id="btn-F">F</a>
        </li>
        <li>
          <a href="#" id="btn-other">other</a>
        </li>
      </ul>
    </li>
    <li class="main-btn" id="btn-list-wrapper">
      <a href="#" id="btn-list">List</a>
      <ul>
        <li>
          <a href="#" id="btn-1list">List 1</a>
        </li>
        <li>
          <a href="#" id="btn-2list">List 2</a>
        </li>
      </ul>
    </li>
    <span class="clear_both"></span>
  </ul>
</div>

Demo at codepen . Codepen进行演示。

Which after execution should become like this 执行后应该变成这样

<div id="selected-div">  
  <div id="lorem-ipsum-wrapper">
    <ul id="lorem-ipsum-main">
      ...
      ...
      ...
    </ul>
  </div>
</div>

How can I achieve this feature using jquery? 如何使用jquery实现此功能? Your help and guidance will be very much appreciated. 非常感谢您的帮助和指导。 Thank you. 谢谢。

Use .html() 使用.html()

$("#selected-div").html('<div id="lorem-ipsum-wrapper">..');

or .load() .load()

$("#selected-div").load("/path/to/html")
var data = '<div id="lorem-ipsum-wrapper">
  <ul id="lorem-ipsum-main">
    <li id="btn-header-wrapper" class="main-btn">
      <a href="#" id="btn-T">T</a>
      <ul>
        <li>
          <a href="#" id="btn-T1">T1</a>
        </li>
        <li>
          <a href="#" id="btn-T2">T2</a>
        </li>
        <li>
          <a href="#" id="btn-T3">T3</a>
        </li>
        <li>
          <a href="#" id="btn-T4">T4</a>
        </li>
      </ul>
    </li>
    <li class="main-btn">
      <a href="#" id="btn-E"><b>E</b></a>
    </li>
    <li id="btn-justify-wrapper" class="main-btn">
      <a href="#" id="btn-gender">Gender</a>
      <ul>
        <li>
          <a href="#" id="btn-M">M</a>
        </li>
        <li>
          <a href="#" id="btn-F">F</a>
        </li>
        <li>
          <a href="#" id="btn-other">other</a>
        </li>
      </ul>
    </li>
    <li class="main-btn" id="btn-list-wrapper">
      <a href="#" id="btn-list">List</a>
      <ul>
        <li>
          <a href="#" id="btn-1list">List 1</a>
        </li>
        <li>
          <a href="#" id="btn-2list">List 2</a>
        </li>
      </ul>
    </li>
    <span class="clear_both"></span>
  </ul>
</div>';   

$("#selected-div").html(data);

您可以像这样使用JQuery text()方法

$("#selected-div").text($('#lorem-ipsum-wrapper').text());

Here is the JS code : 这是JS代码:

$(document).ready(function(){
    $("#selecteddiv").load('add.html');
});

and in add.html, you can have your html code which you want to append. 在add.html中,您可以添加要附加的html代码。

here is the working Plnkr link. 这是有效的Plnkr链接。

hope it helps you 希望对你有帮助

append would also be an option 追加也可以选择

http://api.jquery.com/append/ http://api.jquery.com/append/

$("#selected-div").append($("<h1>Some content</h1>"));

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

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