简体   繁体   English

将HTML代码转换为javascript以进行动态输入

[英]Convert html code to javascript for dynamic entry

I am trying to convert this piece of html code to javascript so that I can dynamically add it to the <div> in the html code. 我正在尝试将这段html代码转换为javascript,以便可以将其动态添加到html代码中的<div>

The html code is
<img src="images/bg_fav.png" alt="" width="199" height="199" class="circle"/>
<a href="#" class="icon"></a>
<h2>Filename</h2>
<ul>
<li><a href="#">Add</a></li>
</ul>

I need to have this code converted into javascript. 我需要将此代码转换为javascript。 I tried doing it like this but it doesn't seem to work: 我试图这样做,但似乎不起作用:

div1 = document.getElementById('div1');
a = document.createElement('img');
a.setAttribute('src','images/bg_shop.png');
a.setAttribute('alt','');
a.setAttribute('width','199');
a.setAttribute('height','199');
a.setAttribute('className','circle');

b=document.createElement('a');
b.setAttribute('className','icon');

c=document.createElement('h2');
c.value="filename";
c.name="filename";
c.id ="filename";

d=document.createElement('ul');
d.innerHTML="<li><a href='#'>add</a></li>"

div1.appendChild(a);
div1.appendChild(b);
div1.appendChild(c);
div1.appendChild(d);

You did never append the div1 element. 您从未添加div1元素。

Some other issues: 其他一些问题:

  • className is a property, not an attribute. className是属性,而不是属性。 If you want to set the attribute, use a plain class instead. 如果要设置属性,请改用普通class
  • Your h2 element is receiving a name , id and value attribute. 您的h2元素正在接收nameidvalue属性。 These attributes make no sense. 这些属性没有意义。 Did you mean: 你的意思是:

     h2.textContent = "filename"; //innerText for IE 
  • Finally, you haven't declared the variables. 最后,您尚未声明变量。 Some browsers refuse to assign values to non-existent variables. 一些浏览器拒绝将值分配给不存在的变量。 Variables are declared by the var keyword: 变量由var关键字声明:

     var div1 = ... 

The syntax is either: 语法为:

a.className = "circle";

OR 要么

a.setAttribute("class","circle");

Never mix-and-match. 切勿混搭。

You're also omitting setting the href attribute of your a tag. 你还忽略设置href您的属性, a标签。

And why are you setting value , name or id on your h2 ? 为什么要在h2上设置valuenameid Do you see <h2 value="filename" name="filename" id="filename" /> ? 您看到<h2 value="filename" name="filename" id="filename" />吗? Of course not. 当然不是。 Either use c.innerHTML = "Filename"; 要么使用c.innerHTML = "Filename"; or the "proper" way: c.appendChild(document.createTextNode("Filename")); 或“适当”的方式: c.appendChild(document.createTextNode("Filename"));

Please use var when you declare a new variable :) 声明新变量时,请使用var :)

Works ok for me providing there is already a div with id of div1 . 如果我已经有一个id为div1div ,那么对我来说可以正常工作。

See this fiddle 看到这个小提琴

Though please also note what Rob and Kolink have noted about your setting of attributes/classes etc. 尽管也请注意Rob和Kolink关于设置属性/类等的内容。

You can just create a string of the required html code and add that to as a html property of parent div. 您只需创建所需html代码的字符串,然后将其添加为父div的html属性即可。 This is simple templating and can work pretty well in most cases. 这是简单的模板,在大多数情况下可以很好地工作。

You should take the approach you are taking only when there is need to edit some property on run time. 仅当需要在运行时编辑某些属性时,才应采用所采用的方法。 For static code, there is no need to take such a complex approach. 对于静态代码,无需采取这种复杂的方法。

function htmlToBeRendered() {
    var html =
         "<img src=\"images\\bg_fav.png\\" alt=\"\" width=\"199\" height=\"199\" class=\"circle\"/>" +
         "<a href=\"#\" class=\"icon\"></a>" +
         "<h2>Filename</h2>" +
         "<ul>" +
             "<li><a href=\"#\">Add</a></li>" +
         "</ul>";
    return html;
};

Have you tried using development tools like http://getfirebug.com/ to debug? 您是否尝试过使用http://getfirebug.com/之类的开发工具进行调试? Is there an element with ID "div1" in your page? 您的页面中是否有ID为“ div1”的元素?

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

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