简体   繁体   English

单击按钮即可动态添加DOM元素

[英]Adding DOM elements dynamically on click of a button

I want to add the DOM elements to a page on click of a button, i have written the code of the things to be added in a new html file called "newmission.html" as shown below. 我想在单击按钮时将DOM元素添加到页面上,我已将要添加的东西的代码编写在称为“ newmission.html”的新html文件中,如下所示。

 <div class="boxheader" ng-controller="edit_vision_missionCtrl">
    <div style="float:right; clear:right; width:20%;">
        <span><a href="" style="" ng-click="deletemission(mission.id)"><img src="assets/img/delete.png" style="width: 20%; height: 20%;"></a></span>
    </div>
    <span style="font-size: large; font-family: monospace; font-weight: bold;">EDIT MISSION NAME</span><input type="text" value="Enter Mission Name" class="form-control" style="background-color: #e8e8e8">
</div>
<div style="padding-top: 10px;">
    <span style="font-size: large; font-family: monospace; font-weight: bold; margin-left:5%;">EDIT MISSION POINTS</span><br />
</div>
<div style="padding-top: 10px;">
    <ul style="float: left; width: 100%;">
        <li style="padding: 2px; width: 100%;">
            <div style="float:right; clear:right; width:20%;">
                <span><a href="" style=""><img src="assets/img/delete.png" style="width: 20%; height: 20%;"></a></span>
            </div>
            <div>
                <input type="text" value="Enter Mission Point" class="form-control" style="background-color: #e8e8e8; width: 80%;">
            </div>
        </li>
    </ul>
</div>
<div style="padding-top: 35px;">
    <a href=""><img id="addmissionpoint" src="assets/img/add.png" alt="addmission" style="width: 7%;height: 12%;float: right; bottom: 0;"></a>
</div>

I am calling a function "addmission" which is in the controller, that adds the above html file contents in a div having id "toappend" but it is not working. 我正在控制器中调用函数“ addmission”,该函数将上述html文件内容添加到ID为“ toappend”的div中,但无法正常工作。 I am using JQuery to add this, but angularJS or Javascript solutions will also be helpful. 我正在使用JQuery进行添加,但是angularJS或Javascript解决方案也将有所帮助。 the function in the controller is shown below. 控制器中的功能如下所示。

var newid = 0;
$scope.addmission = function(){
    newid--;
    $.get("newmission.html", function(data) {
        $("#toappend").prepend(data);
    });
};

This is the code from where i am calling the function. 这是我从中调用该函数的代码。

<div style="padding-top: 35px;">
        <a href="" ng-click="addmission()"><img id="addmission" src="assets/img/add.png" alt="addmission" style="width: 7%;height: 12%;float: right; bottom: 0;"></a>
    </div>

Its is simple using ng-show/ ng-hide 使用ng-show / ng-hide很简单

If you have a button as: 如果您的按钮为:

<button class="primary" ng-click="addmission()" >Addmission</button>

<div class="boxheader" ng-controller="edit_vision_missionCtrl" ng-show="whenAdmission" ng-cloak>
//all other code
</div>

When the event is fired , simple update the When admission flag to true as: 触发事件后,只需将when接纳标志更新为true即可:

$scope.addmission = function(){
    $scope.whenAdmission = true;
};

This will render the cloaked html, otherwise it will remain hidden. 这将呈现隐藏的html,否则将保持隐藏状态。 Do initialize the flag as $scope.whenAdmission = false; 请将标志初始化为$ scope.whenAdmission = false; on page load. 页面加载。

There are several ways of inserting new DOM nodes. 有几种插入新DOM节点的方法。

In your use case, since you have a string of HTML text and want to tell the browser to parse it into HTML elements, you'll want to use innerHTML . 在您的用例中,由于您具有一串HTML文本,并且想要告诉浏览器将其解析为HTML元素,因此您将需要使用innerHTML

var myElement = document.getElementById('myElement')
var someHTML = '<div style="color: blue;">I am <b>blue</b>!</div>"

myElement.innerHTML = someHTML;

This will tell the browser to parse the HTML string into elements and insert them into myElements children tree. 这将告诉浏览器将HTML字符串解析为元素,并将其插入myElements子树中。

Since you're using jQuery you can use jQuery's $(...).html() which will likely call innerHTML anyways. 由于您使用的是jQuery,因此可以使用jQuery的$(...).html() ,无论如何都可能会调用innerHTML

Now since you're using Angular, you may want to load your HTML as a template and render it as a partial instead of directly manipulating the DOM yourself (let Angular do the work). 现在,由于您使用的是Angular,因此您可能希望将HTML作为模板加载并局部渲染,而不是自己直接操作DOM(让Angular来做)。 Here's a good article on loading templates dynamically: http://onehungrymind.com/angularjs-dynamic-templates/ 这是一篇有关动态加载模板的好文章: http : //onehungrymind.com/angularjs-dynamic-templates/

You should never manipulate the DOM from your controller directly. 您永远不要直接从控制器操作DOM。 That's a terrible, terrible idea, and it should NEVER be done. 这是一个可怕的,可怕的想法,绝不应该这样做。 What I would do is to write a simple directive, or if you're really lazy, use ng-include. 我要做的是编写一个简单的指令,或者如果您真的很懒,请使用ng-include。 But for the love of God, don't directly insert stuff into the DOM from a controller. 但是,出于对上帝的爱,请勿直接从控制器将内容插入DOM。 If you write a directive you can keep reusing the same element over and over again in all your projects, and trust me, that saves a lot of time. 如果您编写指令,则可以在所有项目中不断重复使用同一元素,并信任我,这可以节省大量时间。

Learn how to write directives. 了解如何编写指令。 They're a bit convoluted on first blush but they're really powerful feature of Angular. 他们在第一次脸红时有点令人费解,但是它们确实是Angular的强大功能。

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

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