js动态添加HTML标签

来源:互联网 发布:知乎top250 编辑:程序博客网 时间:2024/05/20 03:37
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);};}

原创粉丝点击