在Jquery中获取Span id属性值 [英] Getting the Span id attribute Value in Jquery
问题描述
我有一个"mytable",其中包含"mytd",反过来又包含我要获取的"Myspan" 我想在jquery中获取"myspan_1".我该怎么做.
I have a "mytable" which contains "mytd" inturn contains the "Myspan" i want to get the .I want to get the "myspan_1" in jquery .How do i do that.
小尝试在这里
$("#mytable").find('td > #span').attr("value","myspan_+i");
其中"i"是递增值
我无法获得Span ID.所有iam都尝试使用span标签的"id"值
I coud not get Span id .All iam trying "id" value of span tag
推荐答案
尝试这些
$("#mytable").find("td > span").attr("id");
这将获得当前跨度的id
.如果要为跨度指定id
,则可以执行此操作.
this will get the id
of the current span. if you want to assign an id
to the span you can do this.
$("#mytable").find("td > span").attr("id", "myspan_"+i");
其中i
是增量变量.
此外,如果要向每个td > span
添加ID,则可以在#mytable
的所有td中执行for循环或$.each()
furthermore, if you are adding id to each td > span
you can do a for loop or $.each()
in all td of #mytable
var i=0;
$.each($("#mytable").find("td"), function(){
$(this).find("span").attr("id", "myspan_"+i);
i++;
});
当我阅读你的书名时,我想我想念了你的问题.但我认为这就是您想要的.实际上,如果要在jquery中查找id
,则要在id为#span
的元素中获取属性Value
的值,因为id
始终是唯一的,因此可以直接调用它. >
As I read your title i think I miss understood your question. But I think here's what you want. You want to get the value of the attribute Value
in the element with id of #span
actually if you are looking for an id
in jquery, you can call it directly since id
is always unique.
$("#span").attr("value", "myspan_"+i);
由于i
是一个增量值,因此需要将其放在引号之外.
since i
is an incremental value, you need to put it outside the quotes.
为属性添加值value
$("#mytable").find("td > #span").attr("value", "myspan_"+i);
假设您的span id
具有增量myspan_1
,该增量总是从1开始
Edit 3:
Assuming your span id
has an incremental myspan_1
which always starts at 1
var spanid = $("#mytable").find("td > span").attr("id", "myspan_"+i);
var idvalue = spanid.split('_')[1];
这将获得myspan_1中的1
this will get the 1 of the myspan_1
或者您可以直接获得i
,因为i
是您想要的号码
or you could just directly get the i
since i
is the number that you want
var spanid = $("#mytable").find("td > span").attr("id", "myspan_"+i);
var idvalue = i;
这篇关于在Jquery中获取Span id属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!