使用jQuery获取元素类型 [英] Get element type with jQuery
问题描述
使用jQuery是否有可能使用jQuery找出元素的类型?例如,元素是div,span,select还是input?
Is it possible, using jQuery, to find out the type of an element with jQuery? For example, is the element a div, span, select, or input?
例如,如果我尝试使用jQuery将值加载到下拉列表中,但是同一脚本可以将代码生成到一组单选按钮中,那么我可以创建类似以下内容的东西吗:
For example, if I am trying to load values into a drop-down list with jQuery, but the same script can generate code into a set of radio buttons, could I create something like:
$('.trigger').live('click', function () {
var elementType = $(this).prev().attr(WHAT IS IT);
});
给出一个带有 trigger 类的下拉列表,该列表旁边有一个按钮,当按下按钮时,我的elementType
变量应返回select
.
Given a drop-down list with a button next to it with the trigger class, my elementType
variable should return select
upon the button being pressed.
推荐答案
以jQuery方式获取元素类型:
Getting the element type the jQuery way:
var elementType = $(this).prev().prop('nodeName');
在没有jQuery的情况下做同样的事情
doing the same without jQuery
var elementType = this.previousSibling.nodeName;
检查特定的元素类型:
var is_element_input = $(this).prev().is("input"); //true or false
这篇关于使用jQuery获取元素类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!