javascript - 替换背景图片onclick

标签 javascript jquery html background-image

我正在点击时替换背景图像,但它不起作用。这是我的 html:

 <img id ='pet' src= 'http://ianon.info/pet_support/pic.png' height='200' width='200' style='border: 1px solid black'>;

这是我的 JS+CSS:

 <input type="radio" name="field" value="4" onclick="document.getElementById('pet').style.border='30px solid transparent';document.getElementById('pet').style.border-image='url(http://ianon.info/pet_support/borders/pawprint.png) 120 round'"/> Paw Print Frame &nbsp&nbsp&nbsp

我认为这是我的“onclick”之后的语法问题。任何指示将不胜感激!

最佳答案

JQuery 非常简单且简短。单击单选按钮时替换背景图像。

<script>
    $(document).ready(function(){
        $("[name=field:radio]").click(function(){
            $("[id=pet]").css("border", "30px solid transparent").css("background-image", "url(http://ianon.info/pet_support/borders/pawprint.png) 120 round");
        });
    });
</script>

<img id='pet' src='http://ianon.info/pet_support/pic.png' height='200' width='200' style='border: 1px solid black'>

<input type="radio" name="field" value="4"/> Paw Print Frame &nbsp&nbsp&nbsp

关于javascript - 替换背景图片onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35187466/

相关文章:

javascript - 在 promise 中链接三个查询

jquery - 绕过/阻止 jQuery 中子 div 的 slideUp 事件

javascript - 使用 fullpage.js 在固定元素上滚动后添加或删除 css 类

javascript - 在调整 CSS 以匹配给定规范方面需要帮助

html - 如何使用 Flexbox 以 Angular 并排对齐 2 个不同的组件

javascript - [非] contenteditable HTML5 元素上的键盘事件

javascript - .last() 和 :last 的性能差异

java - 验证 JavaScript 代码

jquery - 将 JQuery Sortable(新顺序)保存到 ASP.Net MVC Controller ?

python - 如何仅打印 HTML 代码中的数字并对其求和?(Python)