记录新项目的数量
这是我的场景,我有一个简化的嵌套可排序树,
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title></title>
</head>
<body>
<ul>
<li id="item_123" name="123">
<fieldset class="additem">
<input type="text" name="title" value="">
</fieldset>
<ul>
<li id="item_253" name="253">
<fieldset class="remove additem">
<input type="text" name="title" value="">
</fieldset>
</li>
<li id="item_252" name="252">
<fieldset class="remove additem">
<input type="text" name="title" value="">
</fieldset>
</li>
<li id="item_250" name="250">
<fieldset class="remove additem">
<input type="text" name="title" value="">
</fieldset>
</li>
<li id="item_247" name="247">
<fieldset class="remove additem">
<input type="text" name="title" value="">
</fieldset>
</li>
</ul>
</li>
</ul>
</body>
</html>
现在看起来像这样,其中字段集有一个 additem 类,我想向树中添加新项目,它看起来像树中的所有其他项目,我可以做到这一点,没有
问题我做的是添加一个小jquery,添加一个按钮并向其附加一个单击事件。
我的大部分代码都在这里,
$(document).ready(function(){
$('<p class="add">Add <img src="/add.png" alt="up.png" /></p>').click(function() {
add_item(this);
}).prependTo("fieldset.additem");
}
function add_item(btn){
var li ='
<li id="item_new'+ X+'" name="new"'+ X+'>'+
'<fieldset class="remove additem">'+
'<input type="text" name="title" value="">'+
'</fieldset>'+
'</li>';
if(!$(btn).parent().next('ul').length) {
$(btn).parents("li:first").append("<ul>"+li+"</ul>");
}else {
$(btn).parent().next("ul").prepend(li);
}
}
请注意,在上面的代码中,我调用了一个变量 X,它需要是一个数字,以便新项目可以是唯一的,这就是我正在寻找的如何跟踪所有新项目我将任何帮助放入我的树中,
我们将不胜感激
编辑 我提到我使用 x 变量似乎很草率,但我也想在以后修改这个函数,这样我就可以用它来添加各种不同的项目,所以我使用 new+ x 或 old + x
here is my scenario I have a nested sortable tree simplified looks like this
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title></title>
</head>
<body>
<ul>
<li id="item_123" name="123">
<fieldset class="additem">
<input type="text" name="title" value="">
</fieldset>
<ul>
<li id="item_253" name="253">
<fieldset class="remove additem">
<input type="text" name="title" value="">
</fieldset>
</li>
<li id="item_252" name="252">
<fieldset class="remove additem">
<input type="text" name="title" value="">
</fieldset>
</li>
<li id="item_250" name="250">
<fieldset class="remove additem">
<input type="text" name="title" value="">
</fieldset>
</li>
<li id="item_247" name="247">
<fieldset class="remove additem">
<input type="text" name="title" value="">
</fieldset>
</li>
</ul>
</li>
</ul>
</body>
</html>
now where the fieldset has an additem class I want to add new items to the tree that will look like all the other items in the tree and I can do that no problem
all i do is add alittle jquery that adds a button and attach a click event to it.
and I have most of my code here
$(document).ready(function(){
$('<p class="add">Add <img src="/add.png" alt="up.png" /></p>').click(function() {
add_item(this);
}).prependTo("fieldset.additem");
}
function add_item(btn){
var li ='
<li id="item_new'+ X+'" name="new"'+ X+'>'+
'<fieldset class="remove additem">'+
'<input type="text" name="title" value="">'+
'</fieldset>'+
'</li>';
if(!$(btn).parent().next('ul').length) {
$(btn).parents("li:first").append("<ul>"+li+"</ul>");
}else {
$(btn).parent().next("ul").prepend(li);
}
}
notice in the above code i call a variable X that needs to be a number so that the new Items can be unique so that is what I'm looking for how to keep track of all the new items I put in to my tree
any help would be appreciated
EDIT
I mentioned that I using an x variable seems sloppy but I would also like to mod this function down the road so that I can us it to add all kinds different items so I ues new+ x or old + x
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于全局变量的讨论。我现在发现我读错了你的代码,我的回答只是针对你的评论,所以它断章取义。
这就是我所说的,如果
x
是在document.ready
中定义的,则它不是全局的:问题的答案
在您提供的 jQuery 代码中,确实 x 需要是全局的,因为
add_item
是在document.ready
之外定义的。这是一种解决方案。将X
和add_item
的定义放在document.ready
中。这样,您就不需要全局范围内的任何内容(尽管由于add_item
采用X
参数,因此它不需要与声明位于同一位置X
)About the discussion of global variables. I see now that I read your code wrong and my answer was only directed at your comment, so it came out out of context.
This is what i meant by
x
not being global if it is defined insidedocument.ready
:The answer to your question
In your provided jQuery code, it's true that x would need to be global, since
add_item
is defined outside ofdocument.ready
. Here's one solution. Put the definition ofX
andadd_item
inside ofdocument.ready
. That way you don't need anything in the global scope (although sinceadd_item
takes anX
parameter, it doesn't need to be in the same place as the declaration ofX
)