简体   繁体   English

在脚本外面写html内容

[英]Write html content outside script

I have below code which will fetch the text from JSON & onclick text, display popup box in page.... Now I need to give options to change font-family, font color etc.... 我有下面的代码将获取JSON和文本的onclick,在页面显示弹出框中的文本 ....现在我需要给选项来改变字体家庭,字体颜色等....

I need to add lot of HTML code in the place of Content .... 我需要在内容的位置添加大量HTML代码....

Is there any way to keep the "content" code in HTML & separate it from script? 有没有办法将“内容”代码保留在HTML中并将其与脚本分开?

在此输入图像描述

JSON : JSON

{        
          "font" : "Arian",
          "x" : 201,
          "y" : 461,          
          "type" : "text",          
          "text" : "Good Food",          
          "name" : "edit_good_1"
}

script 脚本

const lightId = 'light' + layer.name
        const lightIdString = '#' + lightId
        $('.container').append(
            '<a id ="' + layer.name + '" onclick="openPopUp(' + lightId + ')"' +
            '<div class="txtContainer" contenteditable="true" ' +
            'style="' +
            'left: ' + layer.x + 'px; ' +
            'top: ' + layer.y + 'px; ' +            
            '">' + layer.text + '</div></a>' +
            '<div id="light' + layer.name + '" class="white_content" style="' +            
            'top: ' + layer.y + 'px; ' + '"> content <a href="javascript:void(0)" ' +
            'onclick="closePopUp(' + lightId + ')">Close</a></div> <div>'
        );
        document.getElementById(lightId).style.left = layer.x + document.getElementById(layer.name).offsetWidth + 'px'

codepen : https://codepen.io/kidsdial/pen/OGbGwN codepen: https ://codepen.io/kidsdial/pen/OGbGwN

fiddle : https://jsfiddle.net/kidsdial1/z6eyq4j3/ 小提琴: https//jsfiddle.net/kidsdial1/z6eyq4j3/

You can make a separate html file template, and add there some variables, like: 您可以创建一个单独的html文件模板,并添加一些变量,如:

<pre>
<div id="{layer.name}">
<a href="{link}">{link.name}</a>
</div>
....
</pre>

And in the script you can replace your variables with data: 在脚本中,您可以使用数据替换变量:

$.get("/ulrOfYourHtmlTemplate").done(function(template) {
   $('.container').append(template.replace("{layer.name}",layer.name).replace("{link}",link).replace("{link.name}", link.name))
})

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

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