改变图像的src时改变除法的背景图像(Change background image of division while changing src of image)

编程入门 行业动态 更新时间:2024-10-27 10:26:14
改变图像的src时改变除法的背景图像(Change background image of division while changing src of image)

我的文档头部指定了一种样式。 我在分区上指定背景图像,但稍后需要在用户点击链接时更改它,但我尝试做的所有事情都拒绝工作。

<style type="text/css"> /*Some CSS*/ .large { background: url('/images/artwork1.jpg') no-repeat; } </style> <div class="large"></div> <img id="main_image" alt="Image"/> <img onclick="javascript:document.getElementById('main_image').sr‌​c='/images/artwork2.‌​jpg'; $('.large').css({'background-image':'url(/images/artwork2.jp‌​g)';" alt="Change Image"/>

我尝试将背景图像从样式中删除,但点击链接并没有改变它。 我试过JavaScript和jQuery。 我能够更改图像,直到我添加到jQuery中。

I have a style specified in the head of my document. I specify the background image on a division but later on a need to change it if the user clicks on a link but everything I've tried to do refuses to work.

<style type="text/css"> /*Some CSS*/ .large { background: url('/images/artwork1.jpg') no-repeat; } </style> <div class="large"></div> <img id="main_image" alt="Image"/> <img onclick="javascript:document.getElementById('main_image').sr‌​c='/images/artwork2.‌​jpg'; $('.large').css({'background-image':'url(/images/artwork2.jp‌​g)';" alt="Change Image"/>

I tried taking the background image out of the style but clicking on a link didn't change it. I've tried JavaScript and jQuery. I was able to change the image until I added in the jQuery.

最满意答案

是的,那是因为你的语法有误。

尝试这个。

javascript:document.getElementById('main_image').sr‌​c='/images/artwork2.‌​jpg'; $('.large').css('background-image', 'url(/images/artwork2.jp‌​g)');

Yeah, that's because you have error in your syntax.

Try this.

javascript:document.getElementById('main_image').sr‌​c='/images/artwork2.‌​jpg'; $('.large').css('background-image', 'url(/images/artwork2.jp‌​g)');

更多推荐

本文发布于:2023-08-02 14:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1376989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   除法   背景   src   Change

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!