简体   繁体   English

创建DOM元素并追加的替代编写方法

[英]Alternative writing method to create DOM elements and append

If I want to append a button with my pic to the document, I would write: 如果我想将带有图片的按钮添加到文档中,我会写:

$('#story_pages').append('<div><button value="'+window_value+'" onclick="reload_to_canvas(this.value)" > <img id= "w'+window_value+'", src="../pic/white_img.png", width="110px", height="110px"/> </button></div>');

It's too long and hard to debug. 它太长且难以调试。 But how can I create an img tag, then wrapping it with a button tag and div tag... 但是我该如何创建img标签,然后用button标签和div标签包装呢...

Please suggest any clear and simple method with jQuery's help. 请在jQuery的帮助下提出任何简单明了的方法。

UPDATE : story_pages is the jQuery UI dialog's id. 更新 :story_pages是jQuery UI对话框的ID。 I don't know if it affects or not. 我不知道它是否会影响。

UPDATE: I found the problem. 更新:我发现了问题。 I want the image shown above on the button instead of a button and a image. 我想要按钮上显示的图像,而不是按钮和图像。

The script you give me will result this: 您给我的脚本将导致以下结果:

<div>
<button value="1"></button>
<img ......./>
</div>

The img tag has to be wrapped by button tag like: img标签必须由按钮标签包裹,例如:

<button>
    <img.../>
</button>

So the image will attach on the button. 因此图像将附加在按钮上。

How about this: 这个怎么样:

var $button = $('<button>', {
  value: window_value,
  click: function() { reload_to_canvas(this.value); }
});

var $img = $('<img>', {
  id : 'w'+ window_value,
  src: '../pic/white_img.png'
})
.css({ height: '100px', width: '100px'});

$('#story_pages').append($('<div>').append($button, $img));

If a string is passed as the parameter to $(), jQuery examines the string to see if it looks like HTML (ie, it starts with ). 如果将字符串作为参数传递给$(),则jQuery会检查该字符串以查看它是否看起来像HTML(即,以开头)。 If not, the string is interpreted as a selector expression, as explained above. 如果不是,则将字符串解释为选择器表达式,如上所述。 But if the string appears to be an HTML snippet, jQuery attempts to create new DOM elements as described by the HTML. 但是,如果该字符串看起来像是HTML代码段,则jQuery尝试创建HTML所描述的新DOM元素。 Then a jQuery object is created and returned that refers to these elements. 然后,创建并返回一个引用这些元素的jQuery对象。

try this 尝试这个

  var div=$('<div>'); // creates new div element

  //updated here
  var img = $('<img />') .attr({   // create new img elementand adds the mentioned attr
                   id:'w'+window_value , 
                   src:"../pic/white_img.png",
                   width:"110px", 
                   height:"110px"});

  var button= $('<button/>',  //creates new button
  {   
    value: window_value,  //add text to button
    click: function(){ reload_to_canvas(this.value)} //and the click event
  }).html(img);   /// and <-- here... pushed the created img to buttons html


 div.append(button); //append button ,img to div
 $('#story_pages').append(div);   //finally appends div to the selector

updated example fiddle 更新示例小提琴

$('#story_pages').append(
    $('<div>').append(
        $('<button>', {
            value : window_value
        }).click(function() {
            reload_to_canvas(this.value);
        }).append(
            $('<img>', {
                id : 'w' + window_value,
                src : '../pic/white_img.png'
            }).width(110)
              .height(110)
        )
    )
);

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

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