如何访问html元素的属性?

时间:2017-11-14 02:03:34

标签: javascript dom

我正在尝试获取HTML元素的"validacion"属性。

console.log (element[0]);

这让我回报:

enter image description here

<input class="estilo_input_text ng-pristine ng-untouched ng-valid ng-empty" id="asunto" name="asunto" ng-model="asunto" placeholder="Asunto" type="text" validacion="required">

如何访问"validacion"属性?

6 个答案:

答案 0 :(得分:3)

您需要 Element.getAttribute()方法:

console.log(element[0].getAttribute("validacion"));

答案 1 :(得分:1)

尝试element[0].attributes['validacion'].value

祝你有个美好的一天!

答案 2 :(得分:1)

不知道为什么你需要这样做:

您最好使用指令方式获取元素。

或者您可以使用Angular jqLite API:

angular.element('#asunto').attr('validacion')

答案 3 :(得分:0)

使用element[0].getAttribute("attribute_name");方法。

答案 4 :(得分:0)

您可以使用 getAttribute

console.log(element[0].getAttribute("validacion"));

答案 5 :(得分:0)

我认为这是您正在寻找的答案。

由于您正在使用Angular并且您正在实施ng-model="asunto",为什么不在html中将其设为这样。

<input class="estilo_input_text ng-pristine ng-untouched ng-valid ng-empty" id="asunto" name="asunto" ng-model="asunto" placeholder="Asunto" type="text">

<button ng-click="get_model(asunto)"></button>

在你的JS中:

$scope.asunto = "model";

$scope.get_model = function (string) {
  console.log(string)
}

我认为这可能有用,因为您已经在使用ng-model,为什么不使用ng-model

相关问题