使用prototype js更改子图像源

Change child image source using prototype js

本文关键字:图像 prototype js 使用      更新时间:2023-09-26

如何在使用原型点击父链接时更改图标路径?

<a onclick="toggleAmenity('park'); return false;" href="#">
    <input type="image" id="parks_button" src="/images/amenities/icon_parks.gif" name="commit">
</a>

有大约一百万种方法可以剥这只猫的皮,但这可能会给你一些使用原型的想法。

我用的不是"舒适度",而是图像大小。有点令人困惑,但我想在点击时显示一个视觉变化。我强烈建议不要对这种类型的东西(或大多数其他类型的东西)使用onclick属性。

http://jsfiddle.net/gSfmL/

<p>
  <a data-toggle-amenity='64' href="#">
    <input type="image" id="parks_button" src="http://www.google.com/images/icons/product/chrome-128.png" name="commit">
  </a>
</p>
<p>
  <a href="#">This anchor won't fire</a>
</p>​

document.on('click', 'a[data-toggle-amenity]', function(event, element) {
  var amenityToggleType = element.readAttribute('data-toggle-amenity');
  element.down('input').writeAttribute({src: 'http://www.google.com/images/icons/product/chrome-' + amenityToggleType + '.png'});
  element.writeAttribute('data-toggle-amenity', null); // removes the attribute
});​