简体   繁体   English

Javascript:如何将带有html内容的变量注入附加的数据属性?

[英]Javascript : how inject a variable with html content to a data-attribute appended?

I wish to insert a variable that contains an HTML code in a DATA attribute (a href ... data-content= ...) it not work very well because the code inserted deletes some characters and suddenly it does not display properly. 我希望在DATA属性中插入一个包含HTML代码的变量(a href ... data-content = ...),由于插入的代码删除了一些字符,突然间它无法正常显示,因此效果不佳。

Here is the code used 这是使用的代码

        function uploadProgress(file)
        {

            var ext = file.split('.').pop();
            var fileUrl = '/playerFiles/'+file;

            if(ext == 'mp4')
            {
                var preview = '<video autoplay loop muted width="250"><source src="'+fileUrl+'" type="video/mp4">Your browser does not support the video tag.</video>';
            }
            else
            {
                 var preview = '<img src="'+fileUrl+'" width="250">';
            }

            var showtime = $('#'+id).find('td.showtime');
            showtime.html('<a href="#" class="preview" data-toggle="popover" data-html="true" data-content="'+preview+'"><i class="fa fa-file-o"></i> &nbsp; Aperçu</a>');

        }

AND my HTML output return this : 和我的HTML输出返回此:

<a href="#" class="preview" data-toggle="popover" data-html="true" data-content="&lt;img src=" playerfiles="" img_0006.jpg"="" width="250" data-original-title="" title="" aria-describedby="popover45746">"&gt;<i class="fa fa-file-o"></i> &nbsp; Aperçu</a>

Why it doesn't work ? 为什么不起作用? What should I do? 我该怎么办?

Thank You 谢谢

well, lets fix this for the first: you have double quotes in the preview var you should escape them with '\\' eg: 好吧,让我们首先解决此问题:在预览变量中有双引号,应使用'\\'将其转义,例如:

var preview = '<img src=\"' + url + '\" width=\"250\">';

or better use single quotes inside the var 或者最好在var中使用单引号

var preview = "<img src='" + url + "' width='250'>";

but I think it's not good approach to store html in this attr - would be better to store here url only and html in the separate template. 但我认为在此attr中存储html并不是一种好方法-最好在此处仅将url和html存储在单独的模板中。 or render the hidden element on page load 或在页面加载时呈现隐藏的元素

The problem with your code is there is special characters in the preview value . 代码的问题是预览值中包含特殊字符。 If you use code given below then you can override the problem and this is not the proper way and avoid this kind of coding style.Use data attributes for integers,small string values etc.. contents like html or long string values etc either use public properties or hidden controls. 如果使用下面给出的代码,则可以解决问题,这不是正确的方法,请避免使用这种编码样式。将数据属性用于整数,小字符串值等。诸如html或长字符串值等内容都可以使用public属性或隐藏的控件。

function uploadProgress(file)
{

    var ext = file.split('.').pop();
    var fileUrl = '/playerFiles/'+file;

    if(ext == 'mp4')
    {
        var preview = '<video autoplay loop muted width="250"><source src="'+fileUrl+'" type="video/mp4">Your browser does not support the video tag.</video>';
    }
    else
    {
         var preview = '<img src="'+fileUrl+'" width="250">';
    }

    var showtime = $('#'+id).find('td.showtime');
    showtime.html('<a href="#" class="preview" data-toggle="popover" data-html="true" ><i class="fa fa-file-o"></i> &nbsp; Aperçu</a>');

      $(".preview").data("content",preview);


}

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

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