jQuery动态添加元素 [英] Add Element dynamically by jQuery

查看:76
本文介绍了jQuery动态添加元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在尝试单击带有jQuery的按钮时创建新的文本框.我尝试了来自不同网站的几种代码.其中之一是:

Hello,

I am trying to create new text box on click of a button with jQuery. I tried several codes from different websites. One of them is:

var newDiv = $(document.createElement('div')).attr("id", 'my' + num + 'Div');
               newDiv.append().html('<input type="text"  name="TextBox' + num + '" value="TextBox' + num + '" >');
               newDiv.appendTo("#Div1");




仍然当我单击按钮时,什么也没发生




Still when i click on button, nothing happens
What is wrong in my code?

推荐答案

(document.createElement('div')).attr("id",'my'+ num +'Div' ); newDiv.append().html('< 输入 类型 =" 名称 TextBox'+ num +'" TextBox'+ num +'" > '); newDiv.appendTo(#Div1");
(document.createElement('div')).attr("id", 'my' + num + 'Div'); newDiv.append().html('<input type="text" name="TextBox' + num + '" value="TextBox' + num + '" >'); newDiv.appendTo("#Div1");




仍然当我单击按钮时,什么也没发生
我的代码有什么问题?




Still when i click on button, nothing happens
What is wrong in my code?


newDiv.appendTo(#Div1");

这不是使用jquery查找div1.为此,您需要
newDiv.appendTo("#Div1");

This is not using jquery to look up div1. you need


(#Div1").我建议先查找Div1,以使您的代码更具可读性.
("#Div1") for that. I recommend looking up Div1 first, just to make your code more readable.


这篇关于jQuery动态添加元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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