首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我可以使用另一个onclick在一个元素中更改属性值吗?

我可以使用另一个onclick在一个元素中更改属性值吗?
EN

Stack Overflow用户
提问于 2022-11-29 20:15:50
回答 2查看 30关注 0票数 0

我有一个多个缩略图的网页,都是并排放置的。单击图像时,弹出一个模态窗口,显示图像的更大版本和关闭按钮。我希望窗口在单击其中一个图像“按钮”时更改其图像src。因此,我尝试在span元素中放置一个名为"imgsrc“的属性,这些元素都在div的class=”坟场转储“下。

HTML

代码语言:javascript
复制
\\\button that looks like a thumbnail of "image 04"\\\
<button button="click"
        class="graveyard-btn graveyard-primary"
        data-toggle="modal"
        data-target="#exampleModal"
        imgsrc="http://example.com/wp-content/uploads/04.png">
        <img src= "http://example.com/wp-content/uploads/04.png">
    </button>
代码语言:javascript
复制
\\\Image of modal set to "image 22" as default\\\
 <div class="modal-body">
        <img class="modal-image" 
             src="http://example.com/wp-content/uploads/22.png"
             alt="Click on button" />

非工作jQuery

代码语言:javascript
复制
\\\On Click change the modal image src attribute to the value at imgsrc of the thumbnail element clicked\\\
$('.graveyard-dump span').ready(function click(f) {
       $('.graveyard-dump span').click(function click(f) {
            $('.modal-image').attr('src', $(this).attr("imgsrc"));
    });
});

我有了从另一个程序中重用代码的想法,该程序在单击时更改了一系列css元素和文本。以下是我从HTML中窃取的一些代码

代码语言:javascript
复制
///Red button with many attribute values for the jQuery to read and place///
 <span   class="redbtn1" 
                    picChange="url(http://example.com/wp-content/uploads/Resin-Red.png)" 
                    name="Red" 
                    color="#ff0000" 
                    background1="#ff2400"  
                    background2="#ba110c"  
                    background3="#90021F"  
                    background4="none"
                    name1="Scarlet"
                    name2="Crimson"
                    name3="Burgundy"
                    name4="-"
                    ></span>

jQuery

代码语言:javascript
复制
///Reads a click, adds the class "active" and removes it from other buttons, changes many attributes around the page. None of these are attribute-attribute changes though./// 
$('.changecolor span').ready(function click(f) {
        $('.changecolor span').click(function click(f) {
            $('.changecolor span').removeClass("active");
            $(this).addClass("active");
            $('.pic').css('background-image', $(this).attr("picChange"));
            $('.name').text($(this).attr("name"));
            $('.name').css('color', $(this).attr("color"));
            $('.colorbar-1').css('background', $(this).attr('background1'));
            $('.colorbar-2').css('background', $(this).attr('background2'));
            $('.colorbar-3').css('background', $(this).attr('background3'));
            $('.colorbar-4').css('background', $(this).attr('background4'));
            $('.colorbar-1').text($(this).attr("name1"));
            $('.colorbar-2').text($(this).attr("name2"));
            $('.colorbar-3').text($(this).attr("name3"));
            $('.colorbar-4').text($(this).attr("name4"));
EN

回答 2

Stack Overflow用户

发布于 2022-11-29 20:44:53

您可以像这样实现图像源的更改

<img id="display" src=""/>

<button onclick="$('display').src='https://imageLink.com'">Click me</button>

票数 0
EN

Stack Overflow用户

发布于 2022-11-30 20:23:59

解决办法是用

代码语言:javascript
复制
<script>
    $(document).ready(function(){
        $("button").click(function(){
            $("#modal-image").attr("src", $(this).attr("imgsrc"));
  });
});
</script>

我将'.modal-image‘src属性设置为单击按钮的属性"imgsrc“。我还必须删除位于. src中的启动映像,否则它将尝试在第一次单击时加载该映像。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74619796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档