简体   繁体   English

我怎样才能将 append 地址<a>标记为 href</a>

[英]How can I append address to <a> tag href

I use bxslider4 and captions is enabled.我使用 bxslider4 并启用了字幕。 I want to add link to captions.我想添加标题链接。 I done it some but it is applied for just first one.我做了一些,但它只适用于第一个。 How can I append all links to each captions.我怎样才能 append 所有链接到每个标题。 I have edited bxslider javascript file.我已经编辑了 bxslider javascript 文件。

My slider php code:我的 slider php 代码:

foreach ($manset_images as $man_img => $value2) {
                                                  ?>

        <div>
            <a  id="slayt_resim_link" href="index.php?url=content&id=<?=$value2->content_id; ?>">
                <img src="<?php echo URL; ?>public/upload/<?=$value2->content_foto?>" title="<?=$value2->content_title?>" data-url="index.php?url=content&id=<?=$value2->content_id; ?>" style="width:100%; height: 350px;">                                
            </a>
        </div>

    <?php 
    }
?>

I added below code to original bxslider javascript file to get image link and change caption href:我将以下代码添加到原始 bxslider javascript 文件以获取图像链接并更改标题 href:

    // find image link
    var captionLink = $this(this)$(slayt_resim_link).attr('href');
    $('#manset_caption_link').attr('href', captionLink);

This code provides apply for the first caption.此代码提供适用于第一个标题。 I think I can solve the problem if I add captionLink to href="" <a id="manset_caption_link" href=""> but I dont know How can I do.我想如果我将 captionLink 添加到 href="" <a id="manset_caption_link" href="">可以解决问题,但我不知道该怎么做。

Last state of the javascript code: javascript 代码的最后 state 代码:


// bxslider javascript file 719. line   
 /**
     * Appends image captions to the DOM
     */
    var appendCaptions = function() {
      // cycle through each child
      slider.children.each(function(index) {
        // get the image title attribute
        var title = $(this).find('img:first').attr('title');


//------------------ added after ----------------------------
        // find image link
        var captionLink = $this(this)$(slayt_resim_link).attr('href');
        $('#manset_caption_link').attr('href', captionLink);
//------------------------------------------------------------

        // append the caption
        if (title !== undefined && ('' + title).length) {
          $(this).append('<div class="bx-caption"><a id="manset_caption_link" href="---Add captionLink---!!!!!"><span>' + title + '</span></a></div>');
        }

      });
    };

I solved my problem.我解决了我的问题。 Now I can get the all links and append to eah captions.现在我可以获得所有链接和 append 到每个标题。

My php code:我的 php 代码:

foreach ($manset_images as $man_img => $value2) { ?>
    <div>
        <a href="index.php?url=content&id=<?=$value2->content_id; ?>">
            <img src="<?= URL; ?>public/upload/<?=$value2->content_foto?>" title="<?=$value2->content_title?>" data-url="index.php?url=content&id=<?=$value2->content_id; ?>" style="width:100%; height:350px;">                                
        </a>
    </div>
    <?php 
    }
?>

bxslider js last state: bxslider js 最后 state:

    /**
     * Appends image captions to the DOM
     */
    var appendCaptions = function() {
      // cycle through each child
      slider.children.each(function(index) {
        // get the image title attribute
        var title = $(this).find('img:first').attr('title');
        var captionLink = $(this).find('a:first').attr('href');
        // append the caption
        if (title !== undefined && ('' + title).length) {
          $(this).append('<div class="bx-caption"><a id="manset_caption_link" href="'+ captionLink +'"><span>' + title + '</span></a></div>');
        }
      });
    };

The major problem here is that you are using the same id for each slide in your slider.这里的主要问题是您为 slider 中的每张幻灯片使用相同的id You can't have more than one unique id per page, otherwise you will face non standard behaviour.每页不能有多个唯一id ,否则您将面临非标准行为。 Your jQuery snippet will correctly append link to the very first id it finds (which will always be first slide - even though there are more of them).您的 jQuery 片段将正确 append 链接到它找到的第一个id (这将始终是第一张幻灯片 - 即使它们有更多)。
(code below is updated; while I still say that you can't use the same id more than once per page, apparently it breaks original bxslider) (下面的代码已更新;虽然我仍然说每页不能多次使用相同的id ,但显然它破坏了原始 bxslider)

Consider changing your PHP code to use class for a element instead:考虑更改您的 PHP 代码以a class 代替元素:

foreach ($manset_images as $man_img => $value2) { ?>
    <div>
        <a id="slayt_resim_link" class="slayt-resim-link" href="index.php?url=content&id=<?=$value2->content_id; ?>">
            <img src="<?= URL; ?>public/upload/<?=$value2->content_foto?>" title="<?=$value2->content_title?>" data-url="index.php?url=content&id=<?=$value2->content_id; ?>" style="width:100%; height:350px;">                                
        </a>
    </div>
    <?php 
    }
?>



Then your jQuery snippet could look like this:然后您的 jQuery 代码段可能如下所示:

    //------------------ added after ----------------------------
    // find image link
    var captionLink = $(this).find('#slayt_resim_link').attr('href');
    //------------------------------------------------------------

    // append the caption
    if (title !== undefined && ('' + title).length) {
        $(this).append('<div class="bx-caption"><a id="manset_caption_link" href="' + captionLink + '"><span>' + title + '</span></a></div>');
    }



I strongly discourage you from editing original slider files though.我强烈建议您不要编辑原始 slider 文件。 Make your own JS file and add to your project instead, after jQuery and BXSlider were loaded:在加载 jQuery 和 BXSlider 之后,制作您自己的 JS 文件并添加到您的项目中:

(function() {
    var slides = jQuery('.slayt-resim-link');
    slides.each( function() {
        var captionLink = $(this).attr('href');
        $(this).find('.bx-caption a').attr('href', captionLink);
    });
})();

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

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