简体   繁体   English

Jquery - 将html字符串附加到变量中的html字符串

[英]Jquery - appending html string to html string in variable

I am trying to create an HTML string and then modify that HTML string with extra HTML and attributes but it's not working. 我正在尝试创建一个HTML字符串,然后使用额外的HTML和属性修改该HTML字符串,但它不起作用。 What am I doing wrong? 我究竟做错了什么?

$(document).ready(function(){
    $('body').on('click', 'button', function(){
        var thing     = '<div class="thing"></div>';
        var close     = '<a href="#" class="close">close</a>';

        $(thing).append(close);

        $('.canvas').append(thing);

        return false;
    });
});

I did get it to work by combining the strings into one variable before appending them to the DOM but this makes what I'm doing harder to read. 我确实通过将字符串组合到一个变量中然后将它们附加到DOM来实现它,但这使得我正在努力阅读。 Is there any other way of doing this? 有没有其他方法这样做?

Here is a fiddle . 这是一个小提琴

Updated fiddle . 更新了小提琴

You have to assign the return of this expression $(thing).append(close); 你必须分配这个表达式的返回$(thing).append(close); to the variable thing like: 对变量之类的thing

thing = $(thing).append(close);

Else the variable will always hold the default string <div class="thing"></div> as value. 否则变量将始终保持默认字符串<div class="thing"></div>作为值。

Hope this helps. 希望这可以帮助。

 $(document).ready(function(){ $('body').on('click', 'button', function(){ var thing = '<div class="thing"></div>'; var close = '<a href="#" class="close">close</a>'; $('.canvas').append( $(thing).append(close) ); return false; }); }); 
 .thing { width: 50px; height: 50px; background: red; } .close { background: blue; color: white; } .canvas { border: 1px solid black; width: 500px; height: 500px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button>Add Thing</button> <div class="canvas"></div> 

The append method of jquery append to DOM of the page. jquery的append方法附加到页面的DOM。 If you want to make more readble try this: 如果你想做更多的阅读,试试这个:

var thing = '<div class="thing">';
thing    += '<a href="#" class="close">close</a>';
thing    += '</div>';

$('.canvas').append(thing);

You can create new DOM element, instead of string. 您可以创建新的DOM元素,而不是字符串。 This way you can easily append. 这样你就可以轻松追加。 Example: 例:

 $(document).ready(function(){ $('body').on('click', 'button', function(){ var thing = jQuery('<div/>', { class: 'thing' }); var close = jQuery('<a/>', { class: 'close', href: '#', text:'close' }).appendTo(thing); $('.canvas').append(thing); return false; }); }); 
 .thing { width: 50px; height: 50px; background: red; } .close { background: blue; color: white; } .canvas { border: 1px solid black; width: 500px; height: 500px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button>Add Thing</button> <div class="canvas"></div> 

try this 尝试这个

$(document).ready(function(){
$('body').on('click', 'button', function(){
    var thing = '<div class="thing"></div>';
    var close = '<a href="#" class="close">close</a>';
    $('.canvas').append(thing);
    $('.thing').append(close);
    return false;
});

}); });

Just adding some code to @Zakaria Acharki's answer, just in case you want to close the created divs: 只需在@Zakaria Acharki的答案中添加一些代码,以防万一你要关闭创建的div:

 $(document).ready(function(){ $('body').on('click', 'button', function(){ var thing = '<div class="thing"></div>'; var close = '<a href="#" class="close">close</a>'; thing = $(thing).append(close); $('.canvas').append(thing); return false; }); $('body').on('click', '.close', function(){ $(this).parent().remove(); }); }); 
 .thing { width: 50px; height: 50px; background: red; } .close { background: blue; color: white; } .canvas { border: 1px solid black; width: 500px; height: 500px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button>Add Thing</button> <div class="canvas"></div> 

Regards 问候

This is so easy. 这很容易。 you mistaken parseHTML method. 你误认为parseHTML方法。 it parses your html string into jquery object. 它将你的html字符串解析为jquery对象。

The solution is: 解决方案是:

 $(document).ready(function(){ $('body').on('click', 'button', function(){ var thing = '<div class="thing"></div>'; var close = '<a href="#" class="close">close</a>'; var html = $.parseHTML(thing); $(html).append(close); $('.canvas').append(html); return false; }); }); 
 .thing { width: 50px; height: 50px; background: red; } .close { background: blue; color: white; } .canvas { border: 1px solid black; width: 500px; height: 500px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button>Add Thing</button> <div class="canvas"></div> 

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

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