[英]Replace multiple html attributes with javascript
I need to make a function that replaces: addthis:url=
addthis:title=
and addthis:description=
in these 3 elements: 我需要创建一个替换的函数: addthis:url=
addthis:title=
and addthis:description=
in这3个元素:
<a class="addthis_button_facebook_like" fb:like:layout="button_count"
addthis:url="http://example.com"
addthis:title="An Example Title"
addthis:description="An Example Description"></a>
<a class="addthis_button_tweet"
addthis:url="http://example.com"
addthis:title="An Example Title"
addthis:description="An Example Description"></a>
<a class="addthis_button_google_plusone" g:plusone:size="medium"
addthis:url="http://example.com"
addthis:title="An Example Title"
addthis:description="An Example Description"
></a>
How do I do it? 我该怎么做? Eg: with the values newurl.com
, newtitle
and newdescription
? 例如:使用值newurl.com
, newtitle
和newdescription
?
If you give your elements an ID for easy access you can do something like this: 如果您为元素提供ID以便于访问,您可以执行以下操作:
<a id="link1" class="addthis_button_facebook_like" fb:like:layout="button_count" addthis:url="http://example.com" addthis:title="An Example Title" addthis:description="An Example Description"></a>
<script>
var el = document.getElementById("link1");
el.setAttribute("addthis:url", "new url");
el.setAttribute("addthis:title", "new title here");
el.setAttribute("addthis:description", "new description here");
</script>
I'll leave it to you to encapsulate that in a function as required. 我将留给你根据需要将其封装在一个函数中。
Note: if you don't want to or can't give your elements an ID for some reason you could try document.getElementsByClassName()
on your existing HTML, but IE didn't introduce support until IE9. 注意:如果由于某种原因您不想或不能为您的元素提供ID,您可以在现有HTML上尝试document.getElementsByClassName()
,但IE在IE9之前不会引入支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.