简体   繁体   中英

Change attribute value basing on existing value with Javascript

I'll illustrate with an example: I need to convert the following html with javascript

<a href="aaa.kmz"></a>
<a href="eee.kmz"></a>
<a href="rrr.kmz"></a>
...

to code where all href values has changed only the last letter

<a href="aaa.kml"></a>
<a href="eee.kml"></a>
<a href="rrr.kml"></a>
...

Get the a tags, loop through them and replace .kmz with .kml :

​var tags = document.getElementsByTagName("a");

for(var i = 0, l = tags.length; i < l; i++) {
    tags[i].href = tags[i].href.replace('.kmz', '.kml');
}​​​​

Working Example - http://jsfiddle.net/Ln4s4/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM