tagName返回undefined

时间:2016-01-04 02:20:03

标签: javascript jquery

我在一些元素上运行一个循环,并尝试使用我在循环中获取的ID来访问另一组元素我尝试引用其他元素并获取它们的标记,这是我的代码。

function checkRequired(){
    var i = 0;
    $(".required_div").each(function(index){
        if( $(this).html() != '')
        {
            var question_id = $(this).attr('id').substring(9);
            var question_element = $('[name="ry['+question_id+']"');

            console.log(question_element);
            console.log(question_element.tagName);
        }
    });
    console.log(i);
}

这就是我在控制台中获得的每个元素:

1. [textarea#mce_editor_4.tinymce, prevObject: jQuery.fn.jQuery.init[1], context: document, selector: "[name="ry[67]""]
2. undefined

我还尝试使用提到here的prop来访问tagName,但这不起作用,因为它返回question_element.prop is not a function(…)

1 个答案:

答案 0 :(得分:3)

它正在返回.directive('cityStateZip', function() { return { restrict: 'A', transclude: true, scope: { zip: '=', }, template: '<p>{{geoCity}}, {{geoState}} {{geoZip}}</p>', controller: ['$scope', '$timeout', 'GeolocationService', function ($scope, $timeout, GeolocationService) { GeolocationService.geocode($scope.zip).then(function(result) { if (result) { console.log(result); $timeout(function() { $scope.geoZip = result['address_components'][0]['long_name']; $scope.geoCity = result['address_components'][1]['long_name']; $scope.geoState = result['address_components'][2]['short_name']; }); } }); }] }; }) ,因为undefinedjQuery object

你可以访问jQuery对象中的DOM元素,然后获取属性:

question_element

或者您可以使用question_element[0].tagName 方法:

.prop()
相关问题