简体   繁体   English

使用Java脚本将href文本替换为其他内容

[英]Replacing href text with something else using Javascript

How can I replace the "echo ucwords($row2[mainImage1]);" 如何替换“ echo ucwords($ row2 [mainImage1]);” in the href attribute of the tag below with the "echo $row2[selectionThumb3];" 在下面标记的href属性中,带有“ echo $ row2 [selectionThumb3];” from the src attribute of the tag below using Javascript? 从下面的标记的src属性使用Javascript?

Here is how my code looks: 这是我的代码的外观:

    <img class="thumbNotSelected" src="../images/ThumbSelection/<?php echo $row2[selectionThumb3]; ?>.jpg"  />

<div class="mainImage magnify">
     <a href="../images/MainImage/<?php echo ucwords($row2[mainImage1]); ?>.jpg" rel="lightbox" title="<?=$row2[name]?>">
           <img class="mainImage small" src="../images/MainImage/<?php echo $row2[mainImage1]; ?>.jpg" /></a>
</div>

Adding my current Javascript: 添加我当前的Javascript:

$('#thumbs').delegate('img', {
click: function(){
$('.mainImage').attr('src',$(this).attr('src').replace('ThumbSelection','MainImage').replace('selectionThumb','mainImage')); //here I replace the src of the Main Image to match the selected thumbnail.
var $this = $(this),
  index = $this.index();
$("#thumbs img").removeClass('thumbSelected');
 $this.addClass('thumbSelected');

}});

There are several thumbs that the user can click on. 用户可以点击几个大拇指。 The main image loads the corresponding picture after clicking on its Thumb. 单击主图像后,主图像将加载相应的图片。 However, the (used for a lightbox) never changes to adapt to the new loaded picture and the lightbox always uses the same image instead of the one the user has clicked on. 但是,(用于灯箱)永远不会更改以适应新加载的图片,并且灯箱始终使用相同的图像,而不是用户单击的图像。 Thanks! 谢谢!

$('.mainImage a').attr('href', $('.thumbNotSelected').attr('src'));

应该做你所需要的。

With the js below you would be dynamically changing the href of your link to the same exact path of the image loaded. 使用下面的js,您将动态地将链接的href更改为与加载的图像相同的确切路径。 Let me know if it works 让我知道是否有效

 $('#thumbs').delegate('img', {
    click: function(){
        $('.mainImage').attr('src',$(this).attr('src').replace('ThumbSelection','MainImage').replace('selectionThumb','mainImage'));
        $('.mainImage a').attr('href',$(this).attr('src').replace('ThumbSelection','MainImage').replace('selectionThumb','mainImage'));
        var $this = $(this),
        index = $this.index();
        $("#thumbs img").removeClass('thumbSelected');
        $this.addClass('thumbSelected');

    }});

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

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