如何使用javascript从给定的代码段中提取HTML控件的ID? [英] How do I extract id of HTML control from given code snippet using javascript ?
本文介绍了如何使用javascript从给定的代码段中提取HTML控件的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<input style="width: 100%;" name="DateLead_DateEnd" id="DateLead_DateEnd" data-role="datepicker" type="text" class="k-input" role="combobox" aria-expanded="false" aria-owns="DateLead_DateEnd_dateview" aria-disabled="false" aria-readonly="false" aria-activedescendant="f8253ffd-a272-44a8-ba91-6a36b7b536b3_cell_selected">
我的尝试:
我尝试过使用getAttribute方法,但它在浏览器中给出错误
What I have tried:
I have tried using getAttribute method but it gives error in browser
推荐答案
我认为,如果你将通过元素名称或类访问(如果它是唯一的),那么你可以得到id。
//只有当这个元素的名称是唯一的时候
I think, if you will access through element name or class (if it is unique), then you can get the id.
// Only when the name is unique for this element
var inputs = document.getElementsByName("DateLead_DateEnd");
alert(inputs[0].id);
//仅当类名对于此元素是唯一的时
OR
// Only when class name is unique for this element
var inputs = document.getElementsByClassName("k-input");
alert(inputs[0].id);
希望,这对你有用。
Hope, this will help you.
这篇关于如何使用javascript从给定的代码段中提取HTML控件的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文