【问题标题】:How can I set an href for a link to equal a variable inside my script如何将链接的 href 设置为等于脚本中的变量
【发布时间】:2015-12-08 23:52:40
【问题描述】:

我有以下标记来显示 jQuery 滑块内的图像:-

<li>
    <figure> <a href="~/img/big.jpg" class="thumb">
                <img src="~/img/small.jpg" alt=""/>
                <span>
                    <strong>Project name:</strong><em>Villa</em>
                    <img src="~/img/searchsmall.png" alt="" 
                       data-goto="@Url.Action("OurProjects", "Home", new { name = "villaA" })"/>
                </span>
            </a>

    </figure>
</li>

在加载滑块内图像的回调函数中,我想在当前占位符内添加一个新的&lt;a&gt;,其中链接的 href 等于 data-goto 值。

我尝试了以下,

  function loadImage(src, callback) {
      var img = $('<img>').on('load', function () {
          callback.call(img);
      });

      img.attr('src', src);
      var allcaptions = $("figure span");

      // setTimeout is a hack here, since the ".placeholders" don't exist yet
      setTimeout(function () {
          $(".placeholder").each(function (i) {

              // in each .placeholder, copy its caption's mark-up into it
              // (remove the img first)
              var caption = allcaptions.eq(i).clone();

              var t = $(this).find("img").attr('data-goto');

              caption.append("<a href=t>test</a>");
              caption.find("img").remove();
              $(this).append("<div class='caption'>" + caption.html() + "</div>");
          });
      }, 500);
  }

但生成的链接将具有“未定义”作为其href。

编辑

占位符类会在jquery的slider里面,如下

EDIT-2

这是页面加载时的标记:-

<li>
    <figure> 
        <a href="~/img/big.jpg" class="thumb">
            <img src="~/img/small.jpg" alt=""/>
            <span>
                <strong>Project name:</strong><em>Villa</em>
                <img src="~/img/searchsmall.png" alt="" 
                     data-goto="@Url.Action("OurProjects", "Home", new { name = "villaA" })"/>
            </span>
        </a>
    </figure>
</li>

现在,当您单击 small.jpg 图像时,big.jpg 将显示在滑块内。这是滑块内的标记

<div id="galleryOverlay" class="visible" style="display: block;">
<div id="gallerySlider" style="left: 0%;">
<div class="placeholder">
<img src="/img/big.jpg">
<div class="caption">
<div class="caption">
<strong>Project name: </strong>
<em>Villa</em>
<img data-goto="/OurProjects?name=villaA" alt="" src="/img/searchsmall.png">
<a>test</a>
</div>
</div>
<div class="placeholder">
<div class="placeholder">
<div class="placeholder">
<div class="placeholder">
<div class="placeholder">
<div class="placeholder">
<div class="placeholder">
<div class="placeholder"> 

链接没有 href 或 href 未定义。这是我的问题吗?

EDIT-3

这里是 firbug 屏幕

【问题讨论】:

  • 它将以t 作为其href,而不是undefined。
  • @Barmar 当前 "var t = $(this).find("img").attr('data-goto');"正在返回 undefined ...虽然我说要找到 img 然后获取它的 data-goto 属性..!不知道为什么这不起作用?
  • 这越来越令人困惑。我看不出屏幕截图中的 DOM 与问题中的 HTML 有何关系。您需要在代码运行之前而不是之后显示 DOM 的样子。
  • 该标记看起来不正确。你有两个&lt;div class="caption"&gt;,还有很多缺失的&lt;/div&gt;。我猜你想缩写,但你遗漏了太多。
  • 你能做一个 jsfiddle 来演示这个问题吗?

标签: javascript jquery asp.net-mvc


【解决方案1】:

t 似乎是 String 在 caption.append("&lt;a href=t&gt;test&lt;/a&gt;"); ,而不是在上一行引用 t 变量。尝试使用 jQuery( html, attributes ) 的 attributes 参数设置 text , href 属性。

var t = $(this).find("img").attr('data-goto');
caption.append($("<a />", {"text":"test", "href":t}));

【讨论】:

  • 感谢您的回复,但是使用这种方法生成的a 将没有任何href 这里是生成的链接:-
    项目名称: 别墅 测试
  • @guest2713114 似乎在我的情况下变量 t 将是未定义的:-" var t = $(this).find("img").attr('data-goto');"
  • @johnG 预期结果是什么?
  • 预期结果是链接 href 等于 "@Url.Action("OurProjects", "Home", new { name = "villaA" })" 应该是 "/Home/ OurProjects?name=villaA"
  • 尝试使用字符串 "/Home/OurProjects?name=villaA" ?
【解决方案2】:

您需要正确转义 t 中的字符串。 caption.append("&lt;a href=\""+t+"\"&gt;test&lt;/a&gt;");

【讨论】:

    【解决方案3】:

    字符串连接是使用+ 运算符完成的。

    caption.append("<a href='" + t + "'>test</a>");
    

    如果 `t = 'https://jsfiddle.net',生成的锚标记将如下所示

    <a href='https://jsfiddle.net'>test</a>
    

    【讨论】:

    • 似乎是“ var t = $(this).find("img").attr('data-goto');"将未定义不知道为什么?不知道为什么?问题是我使用 Url.Action 作为 data-goto 中的 href 吗?
    • 似乎我需要按如下方式获取 data-goto "var t = caption.find("img").attr('data-goto');"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    相关资源
    最近更新 更多