简体   繁体   English

jQuery和PHP-更改背景图片

[英]JQuery and PHP - change background image

I am trying to get a background image to change on the click of a link. 我正在尝试使背景图片在单击链接时发生变化。 In the page, there is a php function that gets all of the files (just images) from a directory and writes the following for each item: 在页面中,有一个php函数,该函数从目录中获取所有文件(仅是图像),并为每个项目写入以下内容:

print("
    $(\"#photo" . "$curimage\").click(function(e){
    e.preventDefault();
    $(\"#galleryPhoto\").css(\"background-image\", \"url(images/ints/$file)\");
        });
");

The entire code (for just one item) ends up looking like this: 整个代码(仅一项)最终看起来像这样:

<script type="text/javascript">
        $(function(){   

            $("#photo1").click(function(e){
                e.preventDefault(); 
                $("#galleryPhoto").css("background-image", "url(images/ints/001.jpg)");
            });

        return false;    
        }); 
</script>

In the body of the page, there is another php section that does the same thing, but this time it provides the link: 在页面的主体中,还有另一个php节,可以完成相同的操作,但是这次提供了链接:

print("<a id=\"photo" . "$curimage\" href=\"#\" >Change</a>");

This, of course, gives a completed link of: 当然,这给出了完整的链接:

<a id="photo1" href="#" >Change</a>

In addition, there is a div with the id of galleryPhoto, with the following settings in the css: 此外,还有一个id为galleryPhoto的div,其css中具有以下设置:

<div id='galleryPhoto'>
        Main photo here
</div>

#galleryPhoto {
    position: relative;
    height: 300px;
    width: 600px;
    background-color: black;
    background-image: url(../images/ints/blank.jpg);
    background-size: contain;
    background-repeat: no-repeat;
    margin: 20px;
}

But when the link is clicked, the background image does not change. 但是,单击链接后,背景图像不会改变。 I am clueless on what I am doing wrong. 我对做错事一无所知。 I have tried so many different things that the code is probably all messed up now, but I don't know why (JQuery newbie, sorry). 我尝试了很多不同的事情,以至于现在代码可能都被弄乱了,但我不知道为什么(对不起,JQuery新手)。

Can someone please point me in the right direction? 有人可以指出正确的方向吗?

Since your html is being generated dynamically through PHP ensure that you are using the .on function. 由于您的html是通过PHP动态生成的,因此请确保您使用的是.on函数。 So anytime that your html is being generated dynamically it's best to use the .on function. 因此,只要您的html是动态生成的,最好使用.on函数。

So instead of using this: 所以不要使用这个:

 $("#photo1").click(function(e){
      e.preventDefault(); 
      $("#galleryPhoto").css("background-image", "url(images/ints/001.jpg)");
 });

You would use this: 您将使用此:

 $(document).on('click', '#photo1', function(e){
      e.preventDefault(); 
      $("#galleryPhoto").css("background-image", "url(images/ints/001.jpg)");
 });

You have to set the height and width of your #galleryPhoto div because Background image base its height and width on it's container. 您必须设置#galleryPhoto div的高度和宽度,因为背景图片的高度和宽度基于其容器。 Unlike <img> takes the image height and width if not defined <img>不同,如果未定义,则采用图像的高度和宽度

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

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