简体   繁体   English

单击图像以更改按钮的URL

[英]Clicking an image to change the url of a button

How could I make it that if someone clicks the image of the selected product to change the url of the button under the images 如果有人单击所选产品的图像以更改图像下方的按钮的网址,我该怎么做?

<div id="product">
  <img value="1" src="http://placehold.it/300x100?text=Mary+Jane" alt="" />
  <img value="2" src="http://placehold.it/300x100?text=LSD" alt="" />
  <img value="3" src="http://placehold.it/300x100?text=Crack" alt="" />
</div>

<br>

<a id="buy" href="#"><button>Go to the the specific link</button></a>

text/image for amusement only, they are not really the products 文字/图像仅供娱乐,它们并不是真正的产品

What I have tried 我尝试过的

window.onload = function() {
    var sel = document.getElementById('product');
    sel.onchange = function() {
        document.getElementById("buy").href = "https://amazon.com/" + this.value;
    }
}

Here's a codepen http://codepen.io/anon/pen/bdyWGa 这是一个Codepen http://codepen.io/anon/pen/bdyWGa

Jquery solution: jQuery解决方案:

$(function(){
 $("img").click(function(){
    $("#buy").attr("href","https://amazon.com/"+$(this).attr("value"));
 });  
});

Javascript Version: JavaScript版本:

var images = document.getElementsByTagName("img");
for (var i=0, len=images.length, img; i<len; i++) {
   img = images[i];
   img.addEventListener("click", function() {  
      document.getElementById("buy").setAttribute("href", "https://amazon.com/"+ this.getAttribute("value"));
      alert("New href value: " + "https://amazon.com/"+ this.getAttribute("value"));
   });
}

Working fiddle: http://codepen.io/anon/pen/NqVjGY 工作提琴: http : //codepen.io/anon/pen/NqVjGY

<img id="img1" src="http://placehold.it/300x100?text=Mary+Jane" alt="" />
<img id="img2" src="http://placehold.it/300x100?text=LSD" alt="" />
<img id="img3" src="http://placehold.it/300x100?text=Crack" alt="" />

<br>

<a id="buy" href="#"><button>Go to the the specific link</button></a>

and: 和:

$("#img1").on("click", function(){
$("#buy").attr("href","https://mynewurl.net");
});

here you describe clicking on which img will change url to what 在这里,您描述了单击哪个img会将URL更改为

In javascript. 在javascript中。 Use e.target for take the current click element and get the value using attrubutes and apply to the href attribute. 使用e.target获取当前的click元素,并使用attrubutes获取值并将其应用于href属性。

window.onload = function() {
    var sel = document.getElementById('product');
    sel.onclick = function(e) {
      console.log( e.target.attributes[0].value)
     document.getElementById("buy").setAttribute('href', "https://amazon.com/" + e.target.attributes[0].value) ;
    }
}

CodeOpen 代码打开

Use jQuery to add a click event to all images to fetch the property value and add as an addition to href property of the intended element 使用jQuery向所有图像添加click事件以获取属性值,并添加到预期元素的href属性中

 $(function(){ $('img').on('click', function(){ $('#buy').attr('href',"https://amazon.com/" + $(this).attr('value')); }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="product"> <img value="1" src="http://placehold.it/300x100?text=Mary+Jane" alt="" /> <img value="2" src="http://placehold.it/300x100?text=LSD" alt="" /> <img value="3" src="http://placehold.it/300x100?text=Crack" alt="" /> </div> <a id="buy" href="#"><button>Go to the the specific link</button></a> 

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

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