如何使用jQuery获取带增量id的span文本?

时间:2022-11-13 17:40:45

I'm sorry guys I've changed my code to this:

对不起,我已将代码更改为:

<table border="1">
<?php
$a = array('apple','banana','grapes','pineapple');
for ($x=0;$x<=3;$x++)
{
?>
    <tr>
        <td><div class='fruit' ><?php echo $a[$x]; ?></div></td>
        <td><a href='#' class='viewbutton'>View</a></td>
    </tr>
<?php
}
?>    
</table>

Now my problem is how can I get the text inside the <div>? I've tried $(".viewbutton").click(function() { alert ( $('#div_debit').text() ); }); This will return only the first value. How can I get the succeeding values in the array? is it using id?

现在我的问题是如何在

中获取文本?我试过$(“。viewbutton”)。click(function(){alert($('#div_debit')。text());});这将仅返回第一个值。如何在数组中获取后续值?是用id吗?

2 个解决方案

#1


1  

As far as I know, there's no CSS or jQuery-specific selector to match for all numeric values (like [0-9] or \d in regexes); correct me, if I'm wrong!

据我所知,没有CSS或jQuery特定的选择器来匹配所有数值(如[0-9]或正则表达式中的\ d);如我错了请纠正我!

But: as you give each <span> the class "fruit", you can just access them with span.fruit. This will return an array, which you can iterate over with each(). this will be set to the current item within that function, so you can check for the desired ID.

但是:当您给每个类“水果”时,您可以使用span.fruit访问它们。这将返回一个数组,您可以使用each()进行迭代。这将被设置为该功能中的当前项目,因此您可以检查所需的ID。

$('.fruit').each(function(){
    // this.id will be your numeric ID of the current item
});

Also, if your elements are numbered continuously (e.g. 1, 2, 3, and not 2, 1, 3), you can use the :nth-child-pseudo-selector to access one of the <span>s.

此外,如果元素连续编号(例如1,2,3,而不是2,1,3),则可以使用:nth-​​child-pseudo-selector访问其中一个

If all of this doesn't work for you; e.g. if you need to use the class .fruit somewhere else too, the last option I could think of is that you iterate manually over the elements in JavaScript.

如果所有这些对你不起作用;例如如果你还需要在其他地方使用.fruit类,我能想到的最后一个选项就是你在JavaScript中手动迭代这些元素。

for( var x = 0; x <= 3; x++ ) {
    var e = $('#'+x);
    // Do something with the element; or add it to an array of all the elements
}

Edit: If you don't know how many items there will be in the page, but you want to match for every numeric ID, you can use the following.

编辑:如果您不知道页面中将有多少项目,但您想要匹配每个数字ID,则可以使用以下内容。

$('*').each(function(){
    if( this.id.match(/\d+/) ) {
        // do something with an element which has a numeric ID
    }
});

of course, its preferrable to use the above example, if you know how many items there will be, because it's probably faster.

当然,如果你知道会有多少项,它更喜欢使用上面的例子,因为它可能更快。

#2


0  

You are, probably, make it wrong. Don't use id as a number like this, prefer put at least a prefix on it.

你可能会犯错。不要将id用作这样的数字,更喜欢在其上添加至少一个前缀。

You can use $('span.fruit#1').text() to get the first text span. If you don't put a id, you can use $('span.fruit:nth-child(1)').text().

您可以使用$('span.fruit#1')。text()来获取第一个文本范围。如果你没有输入id,你可以使用$('span.fruit:nth-​​child(1)')。text()。

#1


1  

As far as I know, there's no CSS or jQuery-specific selector to match for all numeric values (like [0-9] or \d in regexes); correct me, if I'm wrong!

据我所知,没有CSS或jQuery特定的选择器来匹配所有数值(如[0-9]或正则表达式中的\ d);如我错了请纠正我!

But: as you give each <span> the class "fruit", you can just access them with span.fruit. This will return an array, which you can iterate over with each(). this will be set to the current item within that function, so you can check for the desired ID.

但是:当您给每个类“水果”时,您可以使用span.fruit访问它们。这将返回一个数组,您可以使用each()进行迭代。这将被设置为该功能中的当前项目,因此您可以检查所需的ID。

$('.fruit').each(function(){
    // this.id will be your numeric ID of the current item
});

Also, if your elements are numbered continuously (e.g. 1, 2, 3, and not 2, 1, 3), you can use the :nth-child-pseudo-selector to access one of the <span>s.

此外,如果元素连续编号(例如1,2,3,而不是2,1,3),则可以使用:nth-​​child-pseudo-selector访问其中一个

If all of this doesn't work for you; e.g. if you need to use the class .fruit somewhere else too, the last option I could think of is that you iterate manually over the elements in JavaScript.

如果所有这些对你不起作用;例如如果你还需要在其他地方使用.fruit类,我能想到的最后一个选项就是你在JavaScript中手动迭代这些元素。

for( var x = 0; x <= 3; x++ ) {
    var e = $('#'+x);
    // Do something with the element; or add it to an array of all the elements
}

Edit: If you don't know how many items there will be in the page, but you want to match for every numeric ID, you can use the following.

编辑:如果您不知道页面中将有多少项目,但您想要匹配每个数字ID,则可以使用以下内容。

$('*').each(function(){
    if( this.id.match(/\d+/) ) {
        // do something with an element which has a numeric ID
    }
});

of course, its preferrable to use the above example, if you know how many items there will be, because it's probably faster.

当然,如果你知道会有多少项,它更喜欢使用上面的例子,因为它可能更快。

#2


0  

You are, probably, make it wrong. Don't use id as a number like this, prefer put at least a prefix on it.

你可能会犯错。不要将id用作这样的数字,更喜欢在其上添加至少一个前缀。

You can use $('span.fruit#1').text() to get the first text span. If you don't put a id, you can use $('span.fruit:nth-child(1)').text().

您可以使用$('span.fruit#1')。text()来获取第一个文本范围。如果你没有输入id,你可以使用$('span.fruit:nth-​​child(1)')。text()。