【问题标题】:Custom hyperlink for image displayed through jquery通过 jquery 显示的图像的自定义超链接
【发布时间】:2019-01-23 02:31:07
【问题描述】:

我有一个简单的 html 页面,它根据下拉菜单中的选择显示图像。

<head>
<script src="jquery.min.js"></script>
</head>
<div id="mymenu">
<select id="men" onchange="$('#imageToSwap').attr('src', this.options[this.selectedIndex].value);chngSel()">
<option value="product_a.png" selected>Product A</option>
<option value="product_b.png" selected>Product B</option>
<option value="product_c.png" selected>Product C</option>
</select>
</div>
<img id="imageToSwap" class="profile" src="all_products.png">
</head>
</html>

图像根据所选选项显示。

选择产品B选项(product_b.png显示)时,一旦单击product_b.png,是否可以将超链接更改为product_b.html页面?显示图像的变量href?

【问题讨论】:

    标签: javascript jquery html image dropdown


    【解决方案1】:

    你的 HTML

    <head>
    <script src="jquery.min.js"></script>
    </head>
    <body>
         <div id="mymenu">
           <select id="men">
                <option data-url="/productA" value="product_a.png">Product A</option>
                <option data-url="/productB" value="product_b.png">Product B</option>
                <option data-url="/productC" value="product_c.png">Product C</option>
            </select>
        </div>
        <img id="imageToSwap" class="profile" src="all_products.png">
    </body>
    </head>
    </html>
    

    jQuery

    $(function() {
        var $imageToSwap = $('#imageToSwap');
    
        // change the source of the image
        $('#men').on('change', function(e) {
            var $this = $(this),
                src = $this.val(),
                url = $this.data('url');
    
            $imageToSwap.attr({ src }).data({ url });
        });
    
        // click event for the image
        $imageToSwap.on('click', function() {
            var $this = $(this),
                url = $this.data('url');
    
            location.href = url;
        });
    });
    

    【讨论】:

      【解决方案2】:

      不确定,但您不能同时选择 3 个选定选项。 Selected 用作“默认选择”。尝试只使用一次。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-01
        • 1970-01-01
        • 2018-11-11
        • 1970-01-01
        • 2016-02-03
        • 1970-01-01
        • 1970-01-01
        • 2014-04-10
        相关资源
        最近更新 更多