创建HTML元素并将属性附加到它然后将其包装在另一个元素中的最有效方法是什么? [英] What is the most efficient way of creating an HTML element and appending attributes to it and then wrapping it inside of another element?

查看:65
本文介绍了创建HTML元素并将属性附加到它然后将其包装在另一个元素中的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了这个答案,这很棒。但是如果我还必须围绕该元素包装另一个元素呢?这就是我现在的做法:



$ ('#screenshots')。append($('< a href = '+ link +'title ='+ title +'>< img src ='+ el +'alt =width =200pxheight =200px>< / a> '));



有没有更好的方法这样做?

  document.getElementById(screenshots)。insertAdjacentHTML (beforeend,
'< a href ='+ link +'title ='+ title +'> \ n\
< img src ='+ el +'alt =width =200pxheight =200px> \ n \
< / a>'
);


I found this answer, which is great. But what If I also have to wrap another element around that element? This is how I'm doing it now:

$('#screenshots').append($('<a href="' + link + '" title="' + title + '"><img src="' + el + '" alt="" width="200px" height="200px"></a>'));

Is there a better method of doing it this way?

解决方案

jQuery not needed.

document.getElementById("screenshots").insertAdjacentHTML("beforeend", 
        '<a href="' + link + '" title="' + title + '">\n\
            <img src="' + el + '" alt="" width="200px" height="200px">\n\
        </a>'
    );

这篇关于创建HTML元素并将属性附加到它然后将其包装在另一个元素中的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆