简体   繁体   English

JQuery 不更新背景图像<div>元素?

[英]JQuery not updating the background-image of a <div> element?

When I hover over a picture on this page, I should be updating the larger div element's src attribute above to be the image url of the image I am currently hovering over.当我将鼠标悬停在此页面上的图片上时,我应该将上面较大的 div 元素的 src 属性更新为我当前悬停在其上的图像的图像 url。

My breakpoints reach up to the我的断点达到

"$('#image').on('hover', function() {" "$('#image').on('hover', function() {"

line, but won't actually set the url attribute of the div element on the next line.行,但实际上不会在下一行设置 div 元素的 url 属性。 Any pointers?任何指针?

 function upDate(previewPic) { let newUrl = previewPic.src; $('#image').on('hover', function() { $('#image').attr("url", newUrl); }); };
 <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <div id="image"> Hover over an image below to display here. </div> <img alt="Batter is ready" class="preview" onmouseout="unDo()" onmouseover="upDate(this)" src="https://cdn.sallysbakingaddiction.com/wp-content/uploads/2017/06/moist-chocolate-cupcakes-7.jpg"> <img alt="Perfect Baking" class="preview" onmouseout="unDo()" onmouseover="upDate(this)" src="https://cdn.sallysbakingaddiction.com/wp-content/uploads/2017/06/moist-chocolate-cupcakes-6.jpg"> <img alt="Yummy yummy cup cake" class="preview" onmouseout="unDo()" onmouseover="upDate(this)" src="https://cdn.sallysbakingaddiction.com/wp-content/uploads/2017/06/moist-chocolate-cupcakes-5.jpg">

  1. Remove all inline event handlers删除所有内联事件处理程序
  2. Add mouseenter and leave handlers添加 mouseenter 和 leave 处理程序
  3. Access the css property访问 css 属性

Divs do not have URLs Div 没有 URL

Also I moved the preview to not have to scroll too far down我也将预览移动到不必向下滚动太远

 $(".preview").on("mouseenter",function() { $("#image").css({"background-image": `url(${this.src})`}); // this.src is the DOM notation for the source of the image you are hovering }) $(".preview").on("mouseleave",function() { $("#image").css({"background-image": "" }) })
 #image { height: 500px }
 <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <img alt="Batter is ready" class="preview" src="https://cdn.sallysbakingaddiction.com/wp-content/uploads/2017/06/moist-chocolate-cupcakes-7.jpg" height="50"> <img alt="Perfect Baking" class="preview" src="https://cdn.sallysbakingaddiction.com/wp-content/uploads/2017/06/moist-chocolate-cupcakes-6.jpg" height="50"> <img alt="Yummy yummy cup cake" class="preview" src="https://cdn.sallysbakingaddiction.com/wp-content/uploads/2017/06/moist-chocolate-cupcakes-5.jpg" height="50"> <div id="image"> Hover over an image above to display here.<br/> </div>

You try to set the url (not a valid attribute) of a div.您尝试设置 div 的 url(不是有效属性)。 What you actually want to do is to set the background URL of the div.你真正想要做的是设置 div 的背景 URL。 Check my snippet for a hint into the right direction.检查我的片段以获取正确方向的提示。

Also dont add another event listener inside the update function.也不要在更新函数中添加另一个事件侦听器。

 function upDate(previewPic) { let newUrl = previewPic.src; document.getElementById("image").style.backgroundImage = 'url(' + newUrl + ')'; } function unDo(abc) { document.getElementById("image").style.backgroundImage = 'url()'; }
 #image { width: 100px; height: 120px; background-size: 100px 120px; }
 <div id="image"> Hover over an image below to display here. </div> <img alt="Batter is ready" class="preview" width="50" height="60" onmouseout="unDo()" onmouseover="upDate(this)" src="https://cdn.sallysbakingaddiction.com/wp-content/uploads/2017/06/moist-chocolate-cupcakes-7.jpg"> <img alt="Perfect Baking" class="preview" width="50" height="60" onmouseout="unDo()" onmouseover="upDate(this)" src="https://cdn.sallysbakingaddiction.com/wp-content/uploads/2017/06/moist-chocolate-cupcakes-6.jpg"> <img alt="Yummy yummy cup cake" class="preview" width="50" height="60" onmouseout="unDo()" onmouseover="upDate(this)" src="https://cdn.sallysbakingaddiction.com/wp-content/uploads/2017/06/moist-chocolate-cupcakes-5.jpg">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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