如何使用JavaScript将属性添加到HTML元素 [英] How to add attribute to HTML element using javascript

查看:70
本文介绍了如何使用JavaScript将属性添加到HTML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用javascript(最好不是jquery),我正在尝试更改行:

Using javascript (preferably not jquery) I'm trying to change the line:

<input type="number" name="price" required="" id="id_price">

进入

<input type="number" name="price" required="" id="id_price" step="any">

我知道解决方案必须很简单,但我无法破解。

I know the solution's got to be easy but I just can't crack it. Help would be much appreciated!!

推荐答案

torazaburo 所建议的那样,将不胜感激!注释,您可以使用 setAttribute()方法

document.getElementById("id_price").setAttribute("step","any");

<input type="number" name="price" required="" id="id_price">

div>

OR

首先创建属性并设置值。然后将其添加到元素中。

First create the attribute and set the value. Then add it to the element..

var attr = document.createAttribute('step');
attr.value="any";
document.getElementById("id_price").setAttributeNode(attr);

<input type="number" name="price" required="" id="id_price">

这篇关于如何使用JavaScript将属性添加到HTML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆