<script>
HTMLImageElement.prototype.value =
function
(){
return
this
.getAttribute(
"value"
);
}();
function
say(){
var
vimg1 =document.getElementById(
"img1"
);
alert(vimg1.value);
}
</script>
<img id=
"img1"
value=
"123124"
src=
"https://www.baidu.com/img/bd_logo1.png"
onclick=
"say()"
/>
需要实现点击图片,打印出图片的value属性值。<img />标签和say()方法不能改动。怎么破?
HTMLImageElement.prototype.value =function(){ return this.getAttribute("value"); }();
替换为:
Object.defineProperty(HTMLImageElement.prototype, 'value', { get: function(){ return this.getAttribute('value'); } });
1 <html> 2 <head> 3 <style> 4 body{margin:0px;padding:0px;font-size:12px;} 5 </style> 6 <title>笔试测试</title> 7 <script type="text/javascript" defer="defer"> 8 function say(){ 9 var vimg1 =document.getElementById("img1"); 10 alert(vimg1.getAttribute("value")); 11 } 12 </script> 13 </head> 14 <body> 15 <img id="img1" value="123124" src="https://www.baidu.com/img/bd_logo1.png" onclick="say()"/> 16 </body> 17 </html>
亲测可以弹出value值。。。。