为什么JQuery只影响第一个div元素? [英] Why does Jquery only affect the first div element?

查看:278
本文介绍了为什么JQuery只影响第一个div元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用replace函数去除div中的所有非数字值。



看起来jquery替换只影响第一个元素。

>

这是我的jquery:

$ $ $ $ $'code $('#comment')。 (function(){
var thz = $(this);
var repl = thz.html(thz.html()。replace(/ \D + / g,''));
});

HTML代码:

 < a id =comment1href =#> c2fđf011。 < / A> 
< a id =comment1href =#> c20ff113。 < / A>
< a id =comment1href =#> c201gf76341。 < / A>

结果:


2011年c20ff113。


我想要的结果是:


2011 20113 20176341



解决方案

ID选择器(或任何其他ID选择器,如内部jQuery使用的document.getElementById,因为具有ID的元素由大多数浏览器编入索引并意味着唯一)将仅返回DOM中出现的第一个。将其更改为class并且看到它正在工作:

$ p $ $('。comment')。each(function(){
var thz = $(this); var repl =
thz.html(thz.html()。replace(/ \D + / g,''));
});

HTML

 < a class =comment1href =#> c2fđf011。 < / A> 
< a class =comment1href =#> c20ff113。 < / A>
< a class =comment1href =#> c201gf76341。 < / A>

顺便说一下你的id是这样的: - $ / b
$ b

 < a id =comment1href =#> c2fđf011。 < / A> 
< a id =comment2href =#> c20ff113。 < / A>
< a id =comment3href =#> c201gf76341。 < / A>

开始使用属性选择器可以帮助您(但是会减慢字面速度,因为这是一个属性选择器,失去使用ID的好处)。

  $('[id ^ = comment]')。each(function(){ //使用这个更好的给一个容器上下文$('[id ^ = comment]','container')。each(function ... 
var thz = $(this);
var repl = thz.html(thz.html()。replace(/ \D + / g,''));
});



Demo



道德:ID必须是唯一的

I am using the "replace" function to remove all non-numeric values in a div.

It seems Jquery replace only affects the first element.

Here is my Jquery:

$('#comment').each(function() {
    var thz = $(this);
    var repl = thz.html(thz.html().replace(/\D+/g, ''));
});

HTML Code:

<a id="comment1" href="#"> c2fđf011. </a>
<a id="comment1" href="#"> c20ff113. </a>
<a id="comment1" href="#"> c201gf76341. </a>

Result:

2011 c20ff113. c201gf76341.

The result I want is:

2011 20113 20176341

解决方案

You have duplicate ids, Which is invalid and also jQuery ID selector(or any other id selector like document.getElementById which internally jQuery uses because element with ids are indexed by most browsers and are meant to be unique) will return only the first one that appears in DOM. Change it to class and see it working:

$('.comment').each(function() { 
     var thz =  $(this); var repl =
     thz.html(thz.html().replace(/\D+/g, '')); 
});

HTML

<a class="comment1" href="#"> c2fđf011. </a> 
<a class="comment1" href="#">c20ff113. </a> 
<a class="comment1" href="#"> c201gf76341. </a>

By the way had your id been like this:-

<a id="comment1" href="#"> c2fđf011. </a> 
<a id="comment2" href="#">c20ff113. </a> 
<a id="comment3" href="#"> c201gf76341. </a>

Starts with Attribute selector will help you (But slow you down literally, since this is an attribute selector and lose the advantage of using IDs).

$('[id^=comment]').each(function() { // While using this better give a container context $('[id^=comment]', 'container').each(function...
    var thz = $(this);
    var repl = thz.html(thz.html().replace(/\D+/g, ''));
});

Demo

Moral: IDs must be unique

这篇关于为什么JQuery只影响第一个div元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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