获取具有相同#Id的html元素的文本值 [英] Get text value of html elements with same #Id

查看:198
本文介绍了获取具有相同#Id的html元素的文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<a id="s_6_2_19_0_mb" class="siebui-ctrl-drilldown" name="Name" rowid="1" href="javascript:void(0)">00102400010630001</a>
<a id="s_6_2_19_0_mb" class="siebui-ctrl-drilldown" name="Name" rowid="2" href="javascript:void(0)">00102402608820001</a>

我需要获取这两个元素的文本值.所以我使用以下代码:

I need to get the text values of both these elements. so I use the following code:

$('a#s_6_2_19_0_mb[rowid="1"]').text();
$('a#s_6_2_19_0_mb[rowid="2"]').text();

但是#Id随页面的不同而不同.因此,我在一个变量中获得了id,例如"idVal".并且idVal = s_6_2_19_0_mb.所以现在要获取文本值,我使用了下面的代码

But the #Id will differ from page to page. So I am getting the id in a variable, say "idVal". And idVal = s_6_2_19_0_mb. So now to get the text value I used the below code

$('\'a' +'#'+idVal+'\[rowid\=\"1\"]'+'\'').text();

但是这给我抛出了一个错误.在上述情况下,请让我知道如何使用#id变量提取文本值

But this throws me an error. Please let me know how to extract the text value using a variable for #id in the above case

推荐答案

您可以简化选择器并删除a,并使用以下代码,其中rowId保留要检索的行的ID:

You may simplify your selector and remove the a and use the following code where rowId holds the id of the row you wish to retrieve:

var linkText = $('#'+ idVal +'[rowid=\"'+ rowId +'\"]').text();

或像这样硬编码行ID:

or just hardcode the row id like that:

var link1Text = $('#'+ idVal +'[rowid="1"]').text();
var link1Text = $('#'+ idVal +'[rowid="2"]').text();

干杯

这篇关于获取具有相同#Id的html元素的文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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