简体   繁体   English

从元素复制 HTML,用 jQuery 替换文本,然后附加到元素

[英]Copy HTML from element, replace text with jQuery, then append to element

I am trying to create portlets on my website which are generated when a user inputs a number and clicks a button.我试图在我的网站上创建 portlet,当用户输入一个数字并单击一个按钮时会生成这些 portlet。

I have the HTML in a script tag (that way it's invisible).我在脚本标签中有 HTML(这样它是不可见的)。 I am able to clone the HTML contents of the script tag and append it to the necessary element without issue.我能够克隆脚本标记的 HTML 内容并将其附加到必要的元素,而不会出现问题。 My problem is, I cannot seem to modify the text inside the template before appending it.我的问题是,我似乎无法在追加之前修改模板内的文本。

This is a super simplified version of what I'd like to do.这是我想做的超级简化版本。 I'm just trying to get parts of it working properly before building it up more.我只是想让它的一部分正常工作,然后再建立更多。

Here is the script tag with the template:这是带有模板的脚本标签:

 var p = $("#tpl_dashboard_portlet").html(); var h = document.createElement('div'); $(h).html(p); $(h).find('div.m-portlet').data('s', s); $(h).find('[data-key="number"]').val(s); $(h).find('[data-key="name"]').val("TEST"); console.log(h); console.log($(h).html()); console.log(s); $("div.m-content").append($(h).html());
 <script id="tpl_dashboard_portlet" type="text/html"> <!--begin::Portlet--> <div class="m-portlet"> <div class="m-portlet__head"> <div class="m-portlet__head-caption"> <div class="m-portlet__head-title"> <h3 class="m-portlet__head-text"> <span data-key="number"></span> [<span data-key="name"></span>] </h3> </div> </div> <div class="m-portlet__head-tools"> <ul class="m-portlet_nav"> <li class="m-portlet__nav-item"> <a href="javascript:void(0)" class="m-portlet__nav-link m-portlet__nav-link--icon"><i class="la la-close"></i></a> </li> </ul> </div> </div> <!--begin::Form--> <div class="m-portlet__body"> Found! <span data-key="number"></span> [<span data-key="name"></span>] </div> </div> <!--end::Portlet--> </script>

I'm not sure what I'm doing wrong here.我不确定我在这里做错了什么。 I've tried using .each as well with no luck.我也尝试过使用.each ,但没有运气。 Both leave the value of the span tags empty.两者都将span标签的值留空。

(I've removed some of the script, but the variable s does have a value on it) (我已经删除了一些脚本,但是变量s确实有一个值)

You have two issues here.你在这里有两个问题。 Firstly, every time you call $(h) you're creating a new jQuery object from the original template HTML.首先,每次调用$(h) ,都是从原始模板 HTML 创建一个新的 jQuery 对象。 As such any and all previous changes you made are lost.因此,您之前所做的任何和所有更改都将丢失。 You need to create the jQuery object from the template HTML once , then make all changes to that object.您需要从模板 HTML 创建一次jQuery 对象,然后对该对象进行所有更改。

Secondly, the span elements you select by data-key attribute do not have value properties to change, you instead need to set their text() .其次,您通过data-key属性选择的span元素没有要更改的value属性,您需要设置它们的text() Try this:尝试这个:

 var s = 'foo'; var p = $("#tpl_dashboard_portlet").html(); var $h = $('<div />'); $h.html(p); $h.find('div.m-portlet').data('s', s); $h.find('[data-key="number"]').text(s); $h.find('[data-key="name"]').text("TEST"); $("div.m-content").append($h.html());
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script id="tpl_dashboard_portlet" type="text/html"> <div class="m-portlet"> <div class="m-portlet__head"> <div class="m-portlet__head-caption"> <div class="m-portlet__head-title"> <h3 class="m-portlet__head-text"> <span data-key="number"></span> [<span data-key="name"></span>] </h3> </div> </div> <div class="m-portlet__head-tools"> <ul class="m-portlet_nav"> <li class="m-portlet__nav-item"> <a href="javascript:void(0)" class="m-portlet__nav-link m-portlet__nav-link--icon"><i class="la la-close"></i></a> </li> </ul> </div> </div> <div class="m-portlet__body"> Found! <span data-key="number"></span> [<span data-key="name"></span>] </div> </div> </script> <div class="m-content"></div>

In my case only this is working:在我的情况下,只有这有效:

var template = $('template').clone(true, true); // Copies all data and events
var $h = $('<div />');
$h.html(template);
$h.find('.input-name').attr('value', "your value here"); // Note: .val("your value here") is not working

$('.list').prepend($h.html());

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

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