替换html元素值

时间:2013-03-09 19:32:31

标签: html

我试图提示取代“数据量”值的值,你会怎么做呢?

<html>

<body>

<a href="http://google.com/" class="dwolla_button" data-name="Lease" data-desc="Payments" data-amount="4" data-shipping="0" data-tax="0" data-guest-checkout="true" data-key="xxxxx">Bank </a>


<script type="text/javascript" src="https://www.dwolla.com/scripts/button.min.js"> </script>

</body>

</html>

我试图以这种方式使用javascript替换

<script>
document.getElementByClass("dwolla_button").data-amount="43";
</script>

并且考虑过像这样使用jquery但是不知道如何在第一时间加载jquery

$("a").attr("data_amount", "43")

有什么想法吗?

我完全尝试的整个代码是

<html>
  <body>

    <a href="http://249lease.webs.com/" class="dwolla_button" data-name="Lease" data-desc="Payments" data-amount="5" data-shipping="0" data-tax="0" data-guest-checkout="true" data-key="sHqCOc31Vh3JiAsPhBfEk5f4D9bAWKLzwaT06ah3Aw65i0gkZl">Bank Account (25¢)</a>


<script type="text/javascript">

document.getElementByClass("dwolla_button")[0].setAttribute("data-amount", "43");
</script>  

    <script type="text/javascript" src="https://www.dwolla.com/scripts/button.min.js"> </script>

  </body>
</html>

3 个答案:

答案 0 :(得分:1)

简单的JS解决方案是:

document.getElementsByClassName("dwolla_button")[0].setAttribute("data-amount", "43");

如果dwolla_button是页面中的第一个(或唯一)dwolla_button,那么哪个会有用。

答案 1 :(得分:0)

如果你已经有了jQuery,jQuery.data()就是你要找的东西:

$(".dwolla_button").data("amount", 43)

它将使用受支持的HTML 5的data属性及其自己的实现 在较旧的浏览器中。

答案 2 :(得分:0)

包含Google为加载jquery提供的脚本: -

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>