首頁  >  文章  >  web前端  >  在img添加新属性_html/css_WEB-ITnose

在img添加新属性_html/css_WEB-ITnose

WBOY
WBOY原創
2016-06-24 12:20:071075瀏覽

HTML JavaScript

部分HTML:
  
在img添加新属性_html/css_WEB-ITnose

   

        
  • 在img添加新属性_html/css_WEB-ITnose

  •     
  • 在img添加新属性_html/css_WEB-ITnose

  •     
  • 在img添加新属性_html/css_WEB-ITnose

  •     
  • 在img添加新属性_html/css_WEB-ITnose

  •     
  • 在img添加新属性_html/css_WEB-ITnose

  •    

部分javascript
//鼠标经过小图大图变化

$('.xiaotu_box img').mouseover(function(){
document.getElementById("main_img").src=this.dataLarge;
})
结果会报错,原因应该是img中没dataLarge属性,那如何添加呢?
求解答,感激不尽!

回复讨论(解决方案)

document.getElementById("main_img").src= $(this).attr('dataLarge');

$('.xiaotu_box img').mouseover(function(){
$('#main_img').attr('src' , $(this).attr('dataLarge'));
})

用了jQuery就尽量用jQuery写,不要一下jQuery,一下document.getXXXX

$('.xiaotu_box img').mouseover(function(){
$('#main_img').attr('src' , $(this).attr('dataLarge'));
})

用了jQuery就尽量用jQuery写,不要一下jQuery,一下document.getXXXX

因为我觉得现在刚开始从事前端开发,想多用些javascript,jQuery只是觉得它的选择器挺好用的采用的,不过你说的也很有道理,呵呵!

$('.xiaotu_box img').mouseover(function(){
$('#main_img').attr('src' , $(this).attr('dataLarge'));
})

用了jQuery就尽量用jQuery写,不要一下jQuery,一下document.getXXXX
呃  那假如不用jquery而是只用javascript的话,应该怎么才不出错呢?

$('.xiaotu_box img').mouseover(function(){
$('#main_img').attr('src' , $(this).attr('dataLarge'));
})

用了jQuery就尽量用jQuery写,不要一下jQuery,一下document.getXXXX
用这个也可以
document.getElementById("main_img").src=this.getAttribute('dataLarge');

谢谢你了!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn