简体   繁体   English

无法更改 jQuery 中 div 的背景图像

[英]Can't change background image of div in jQuery

I added an event in my script that attempts to set the background-image of a div when a button is clicked.我在脚本中添加了一个事件,该事件尝试在单击按钮时设置 div 的背景图像。 However, it is not working.但是,它不起作用。

Here is the code:这是代码:

HTML HTML

<div class="col-md-2 imageCard" style="float:left">
            <img class="card-img-top"
              src="https://mdbootstrap.com/img/Photos/Horizontal/City/4-col/img%20(60).jpg" alt="Card image cap">
</div>

<div class="card-body">
          <blockquote class="blockquote mb-0">
            <p id="quoteSample">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer posuere erat a ante.</p>
            <footer class="blockquote-footer" id="quoteAuthorSample">Someone famous in </footer>
          </blockquote> 
</div> 

JS JS

$('.imageCard').click(function(){
    var imageSRC = ($(this).children('img').attr('src'));
    console.log(imageSRC)
    $('.card-body').css("background-image", "url(" + imageSRC + ")")
})

Console.log returns: https://mdbootstrap.com/img/Photos/Horizontal/City/4-col/img%20(60).jpg Console.log 返回: https://mdbootstrap.com/img/Photos/Horizontal/City/4-col/img%20(60).jpg

It's like the button element $('.imageCard') isn't ready when the script tries to set the event listener (in this case: click ).当脚本尝试设置事件侦听器(在本例中为: click )时,就像按钮元素$('.imageCard')尚未准备好。

For that, you have two options:为此,您有两种选择:

  1. Put your script tag on the bottom of the page, that way they will load just before the HTML, so it will find the button that was already rendered.将您的脚本标签放在页面底部,这样它们将在 HTML 之前加载,因此它会找到已经呈现的按钮。
  2. Put your jquery click on a window load event, you can use like this:把你的 jquery 点击一个 window 加载事件,你可以这样使用:

     $(document).ready(function() { // Jquery click event });

    this code make sure that your javascript DOM code runs only when the document (HTML page) is rendered completely此代码确保您的 javascript DOM 代码仅在文档(HTML 页面)完全呈现时运行

I hope it helps!!我希望它有帮助!

I find it out, the problem is in the concatenation assignment, the background-image CSS property can receive an URL by adding url() but this url() needs to receive a string, it works if you use it in plain js, but since you are adding this property by a function (using strings in the parameters), you need to be careful with the final return, example (you can try it on your browser console):我发现了,问题出在串联分配中, background-image CSS 属性可以通过添加url()接收 URL 但是这个url()需要接收一个字符串,如果你在纯 js 中使用它,它可以工作,但是由于您通过 function 添加此属性(在参数中使用字符串),因此您需要小心最终返回,例如(您可以在浏览器控制台上尝试):

assign a string (link) to a variable将字符串(链接)分配给变量

    var imageSRC = "https://mdbootstrap.com/img/Photos/Horizontal/City/4-col/img%20(60).jpg"

and just type that, the return will be something like只需输入,返回将类似于

    "url(" + imageSRC + ")" // "url(https://mdbootstrap.com/img/Photos/Horizontal/City/4-col/img%20(60).jpg)"

Now, pay attention to the link inside the url() , it's not a string anymore, is just text, and url() must receive a string现在,注意url()里面的链接,它不再是字符串,只是文本,并且url()必须接收一个字符串

You can solve this in two ways:您可以通过两种方式解决此问题:

  1. Use "url('" + imageSRC + "')" instead of "url(" + imageSRC + ")" that way you'll be putting the correct quotes on the parameter使用"url('" + imageSRC + "')"而不是"url(" + imageSRC + ")"这样你就可以在参数上加上正确的引号
  2. Use javascript template strings (you can read more about it here: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/template_strings )使用 javascript 模板字符串(您可以在此处阅读更多信息: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/template_strings

     `url("${imageSRC}")`

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

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