简体   繁体   English

Javascript-将HTML写入“附加”函数的更好方法

[英]Javascript - better way to write html into “append” function

say I have the following code: 说我有以下代码:

var rightPane = $("#right");
    // Draw the right pane
    rightPane.html('<h2>' + task.task + '<h2> <hr />' +
                     'data1: <input type="text" id="data1" value="' + task.data1 + '" /> <br />' + 
                     'data2: <input type="text" id="data2" value="' + task.data2 + '" /> <br />' + 
                     'data1: <input type="text" id="data3" value="' + task.data3 + '" /> <br />' + 
                     '<input type="button" id="subUpdated" value="Save">');

I there any way to write the HTML code like a simple HTML code , and without the qoutes and the plus signs? 我有没有办法像简单的HTML代码一样编写HTML代码,并且没有qoutes和加号?

In the current version of JavaScript, you can do this by escaping the newlines at the ends of lines, which is a bit better but note that leading whitespace will be included in the string, and you still have to use concatenation to swap in your values: 在JavaScript的当前版本中,您可以通过在线路的两端逃离换行,这是一个好一点 ,但注意开头的空白将被包括在字符串中做到这一点,你还是要用串联在你的价值交换:

var rightPane = $("#right");
// Draw the right pane
rightPane.html('<h2>' + task.task + '<h2> <hr />\
data1: <input type="text" id="data1" value="' + task.data1 + '" /> <br />\
data2: <input type="text" id="data2" value="' + task.data2 + '" /> <br />\
data1: <input type="text" id="data3" value="' + task.data3 + '" /> <br />\
<input type="button" id="subUpdated" value="Save">');

In the next version of JavaScript, "ES6", we'll have template strings which can be multi-line and which let you easily swap in text using ${...} : 在下一个JavaScript版本“ ES6”中,我们将提供模板字符串 ,该模板字符串可以是多行的,并允许您使用${...}轻松交换文本:

// REQUIRES ES6
var rightPane = $("#right");
// Draw the right pane
rightPane.html(`
    <h2>${task.task}<h2> <hr />
    data1: <input type="text" id="data1" value="${task.data1}" /> <br /> 
    data2: <input type="text" id="data2" value="${task.data2}" /> <br /> 
    data1: <input type="text" id="data3" value="${task.data3}" /> <br /> 
    <input type="button" id="subUpdated" value="Save">
    `);

(Again leading whitespace is included in the string.) (再次在字符串中包含前导空格。)

To do that before ES6, you can use any of several templating libraries (Handlebars, Mustache, RivetsJS). 为此,可以在ES6之前使用多个模板库中的任何一个(Handlebars,Mustache,RivetsJS)。

For a really simple version, you could use the function I wrote for another question. 对于一个非常简单的版本,您可以使用为另一个问题编写的函数

You could create your own super-simple 'template engine'. 您可以创建自己的超简单“模板引擎”。

  • Write your template in the markup of your page as you would a usual element. 像平常一样,将模板写在页面的标记中。
  • Give the template container element an attribute to denote its role as a template, for example a data attribute of data-template . 给模板容器元素一个属性,以表示其作为模板的角色,例如data-template的data属性。
  • Clone and detach all data-template elements when the DOM is ready. DOM准备好后,克隆并分离所有data-template元素。
  • On some event, insert your values as you like, then re-insert the compiled template into the page. 在某些情况下,请根据需要插入值,然后将已编译的模板重新插入页面。

For example: 例如:

 $(document).ready(function() { var template = $('[data-template]').detach().clone(); var values = { foo: 'bar', yipee: 'yahoo' }; $('button').click(function() { for(var prop in values) { template.find('[data-value="' + prop + '"]').html(values[prop]); $('#container').append(template); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button>Insert</button> <div id="container"></div> <div data-template> <div data-value="foo"></div> <div> <div data-value="yipee"></div> </div> </div> 

Not currently, other than templates as others have mentioned. 目前还没有,除了其他人提到的模板。 What you could do, however, is to include the initial HTML code in your main document, and then set the values with rightPane.val(task.data1); 但是,您可以做的是在主文档中包含初始HTML代码,然后使用rightPane.val(task.data1);设置值rightPane.val(task.data1); , etc.

You can try to do something like this: Format a string using placeholders and an object of substitutions? 您可以尝试执行以下操作: 使用占位符和替换对象设置字符串格式?

but as Hacketo suggests it would be better to learn to use templates. 但是正如Hacketo所建议的那样,最好学习使用模板。 Take for instance look at underscore.js ( http://underscorejs.org/#template ) 例如看一下underscore.js( http://underscorejs.org/#template

One alternative way of approaching this would be : 解决此问题的另一种方法是:

var $h2     = $("<h2>", {text : task.task});
var $data1  = $("<input>", {type : 'text', value : task.data1});
var $data2  = $("<input>", {type : 'text', value : task.data2});
var $data3  = $("<input>", {type : 'text', value : task.data3});
var $button = $("<input>", {type : 'button', value : 'Save'});

rightPane.append($h2, $data1, $data2, $data3, $button);

This might be a little easier to follow when scanning over the code, rather than shifting through the .html() function. 扫描代码时,可能比在.html()函数中移动要容易一些。

You could take a look at a JavaScript templating engine. 您可以看一下JavaScript模板引擎。 Such as Handlebars, Mustache etc. You can see some of the popular ones here : You can kind of view these as user controls similar to what you get in ASP.NET 如车把,胡子等,你可以看到一些流行的在这里 :你可以查看这些以用户控件类似于您在ASP.NET得到什么

This is an example of Handlebars for your code: 这是您的代码的把手示例:

<h2>{{task}}<h2> <hr />
data1: <input type="text" id="data1" value="{{data1}}" /> <br /> 
data2: <input type="text" id="data2" value="{{data2}}" /> <br /> 
data1: <input type="text" id="data3" value="{{data3}}" /> <br /> 
<input type="button" id="subUpdated" value="Save">

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

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