您的位置:首页 > Web前端 > JavaScript

js动态添加HTML标签

2017-12-01 11:09 423 查看
function addElement_imageText(img,title,source,view_count,publish_time,url,article_type,newsid,category_chn){
/*var reconmendDiv =  document.getElementById("m-newList");
var reconmendDivWarp = document.createElement("div");
reconmendDivWarp.setAttribute("class", "wrap");
reconmendDiv.appendChild(reconmendDivWarp);
var ul = document.createElement("ul");
reconmendDivWarp.appendChild(ul);*/

var ul = document.getElementById("newListul");
var li = document.createElement("li");
li.setAttribute("class", "fl image-text-li");
ul.appendChild(li);
var lidiv = document.createElement("div");
lidiv.setAttribute("class", "newsdetail");
li.appendChild(lidiv);

var imgdiv = document.createElement("div");
imgdiv.setAttribute("class", "fl image-text-li");
var liimg = document.createElement("img");
liimg.src = img;
lidiv.appendChild(liimg);

if(itypeItem==1||itypeItem==undefined){
var typediv = document.createElement("div");
if(category_chn==""||category_chn.length>3){
category_chn = "其他";
}
typediv.innerHTML = category_chn;
typediv.setAttribute("class", "typetext");
lidiv.appendChild(typediv);
}
var lip = document.createElement("p");
lip.innerHTML = title.substring(0,25);
lidiv.appendChild(lip);
var span1 = document.createElement("span");
span1.setAttribute("class", "firstspan");
span1.innerHTML = source.substring(0,5);
lidiv.appendChild(span1);
var span2 = document.createElement("span");
span2.innerHTML = view_count+"评论";
span2.setAttribute("class", "secendspan");
lidiv.appendChild(span2);
var span3 = document.createElement("span");
span3.setAttribute("class", "lastspan");
span3.innerHTML = publish_time;
lidiv.appendChild(span3);

li.onclick=function(){//给p标签添加onclick事件
Avatar.push(['track', ['click', 'detail', '', {tab:category_chn, id:newsid}, 0]]);
window.open(url);
};
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  js添加html标签