[英]how to get the id of html element in javascript or jquery?
<div class="class1" co="51">
<div class="class2">
<label class="class3">
<input class="4" dataid="8d2" name="dd" type="radio" />
<span class="quiz-option-text">ff</span>
</label>
</div>
</div>
i have some thing like this , I want id of the INPUT in javascript or jquery how can i do it?我有这样的事情,我想要 javascript 或 jquery 中的 INPUT id 我该怎么做?
Using javascript:使用javascript:
var radioInput = document.getElementsByName("dd")[0];
var radioInpId = radioInput.id; //will give you id
First your input should have an id attribute首先你的输入应该有一个 id 属性
Like this像这样
<Input id="ggg" type="text">
Then you get it as the following in plain javascript:然后你在普通的javascript中得到如下:
var myInput =document.getElementById("ggg");
Or using jQuery:或者使用 jQuery:
var myInput= $("#ggg");
If you want to get all dataid in document you have to go through all the elementals in the document.如果您想获取文档中的所有 dataid,您必须遍历文档中的所有元素。 see Demo看演示
Example HTML示例 HTML
<div class="class1" co="51">
<div class="class2">
<label class="class3">
<input class="4" dataid="8d2" name="dd" type="radio" />
<span class="quiz-option-text">ff</span><br>
<input dataid="8d3" name="dd" type="text" /> <br>
<input class="4" dataid="8d4" name="dd" type="text" /> <br>
<textarea dataid="8d5">fdfd</textarea> <br>
</label>
</div>
</div>
Jquery查询
<script type="text/javascript">
$(document).ready(function(){
var id_arr=new Array();
var i=0;
$('textarea,input').each(function(){
var id=$(this).attr("dataid");
alert(id);
id_arr[i]=$(this).attr("dataid");
i++;
});
alert(id_arr);
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.