如何使用 Jquery 替换添加到页面的新元素

发布于 2024-09-26 06:06:31 字数 452 浏览 7 评论 0原文

这是场景...我在每个字段旁边都有一个复选框,我在页面加载时用带有“删除”文本的 jquery 替换该复选框,使我能够通过 jquery 删除该字段,这工作正常。就像这样...

$(".profile-status-box").each(function(){ $(this).replaceWith('<span class="delete">' + 'Delete' + '</span>') });

但是问题是,页面加载后,我还为用户提供了动态添加新字段的选项。新添加的字段虽然具有复选框,但没有删除链接,因为它们不会被 jquery 替换,因为它们是在初始页面加载后添加的。

是否可以在不刷新页面的情况下替换添加到页面的新元素的内容?如果没有,我总是可以有两个具有不同标记的模板,具体取决于一个用于 js,一个用于非 js,但我试图避免这种情况。

提前致谢。

Here is the scenario... I have a a checkbox next to each field that I am replacing on page load with jquery with "Delete" text that enables me to delete the field via jquery, which is working fine. Like so...

$(".profile-status-box").each(function(){ $(this).replaceWith('<span class="delete">' + 'Delete' + '</span>') });

The problem comes in however is that after page load, I am also giving the user the option to dynamically add new fields. The new added fields though have the checkbox and not the delete link because they are not being replaced by jquery since they are being added after the initial page load.

Is't possible to replace the content of new elements added to the page without doing a page refresh? If not, I can always have two templates with different markup depending one for js and one for non js, but I was trying to avoind taht.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

思念绕指尖 2024-10-03 06:06:31

您可以使用 .livequery() 插件,如下所示

$(".profile-status-box").livequery(function(){ 
  $(this).replaceWith('<span class="delete">Delete</span>') 
});

:匿名函数针对找到的每个元素以及添加时与选择器匹配的每个新元素运行。

You can use the .livequery() plugin, like this:

$(".profile-status-box").livequery(function(){ 
  $(this).replaceWith('<span class="delete">Delete</span>') 
});

The anonymous function is run against every element found, and each new element matching the selector as they're added.

意中人 2024-10-03 06:06:31

看看这个很酷的演示。它删除和添加元素,如魅力。

http://www.dustindiaz.com/basement/addRemoveChild.html

操作方法如下:

首先最重要的是,(x)html 非常简单。
xHTML Snippet

<input type="hidden" value="0" id="theValue" />
<p><a href="javascript:;" onclick="addElement();">Add Some Elements</a></p>
<div id="myDiv"> </div>

隐藏的输入元素只是让您有机会动态呼叫您可以开始的号码。例如,可以使用 PHP 或 ASP 进行设置。 onclick 事件处理程序用于调用该函数。最后,div 元素已设置并准备好接收附加到其自身的一些子元素(天哪,听起来很奇怪)。

Mkay,到目前为止很容易。现在JS函数了。

addElement JavaScript 函数

function addElement() {
  var ni = document.getElementById('myDiv');
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  numi.value = num;
  var newdiv = document.createElement('div');
  var divIdName = 'my'+num+'Div';
  newdiv.setAttribute('id',divIdName);
  newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\'#\' onclick=\'removeElement('+divIdName+')\'>Remove the div "'+divIdName+'"</a>';
  ni.appendChild(newdiv);
}

如果您愿意,

removeElement JavaScript 函数

function removeElement(divNum) {
var d = document.getElementById('myDiv');
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);
就是

这样。鲍勃你叔叔。

这取自这篇文章/教程: http:// /www.dustindiaz.com/add-and-remove-html-elements-dynamically-with-javascript/

我自己刚刚学会了这一点。谢谢你的问题

希望有帮助。

PK

Have a look at this kool demo. It removes and adds elements like a charm.

http://www.dustindiaz.com/basement/addRemoveChild.html

Here's how:

First of all, the (x)html is real simple.
xHTML Snippet

<input type="hidden" value="0" id="theValue" />
<p><a href="javascript:;" onclick="addElement();">Add Some Elements</a></p>
<div id="myDiv"> </div>

The hidden input element simply gives you a chance to dynamically call a number you could start with. This, for instance could be set with PHP or ASP. The onclick event handler is used to call the function. Lastly, the div element is set and ready to receive some children appended unto itself (gosh that sounds wierd).

Mkay, so far so easy. Now the JS functions.

addElement JavaScript Function

function addElement() {
  var ni = document.getElementById('myDiv');
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  numi.value = num;
  var newdiv = document.createElement('div');
  var divIdName = 'my'+num+'Div';
  newdiv.setAttribute('id',divIdName);
  newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\'#\' onclick=\'removeElement('+divIdName+')\'>Remove the div "'+divIdName+'"</a>';
  ni.appendChild(newdiv);
}

And if you want to,

removeElement JavaScript Function

function removeElement(divNum) {
var d = document.getElementById('myDiv');
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);
}

and thats that. bobs your uncle.

This is taken from this article/tutorial: http://www.dustindiaz.com/add-and-remove-html-elements-dynamically-with-javascript/

I've just learnt this myself. thank you for the question

Hope that helps.

PK

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文