【问题标题】:How to get href's caption and display it into a span?如何获取href的标题并将其显示为跨度?
【发布时间】:2013-10-08 14:49:16
【问题描述】:

我正在使用 jquery 在 div 上加载不同的 html 页面。我想要做的是抓住点击链接的标题并将其显示到一个跨度中(类似于fancybox功能)

$(".iframe").on("click", function(e) {
        e.preventDefault();
        var $mylink = $(this).attr('href');
        $mydiv.load($mylink)
}); 

html

<a class="iframe" href="page.html" caption="My title">

【问题讨论】:

    标签: jquery html hyperlink title


    【解决方案1】:

    也许你已经尝试过了。希望对你有所帮助。

    jsfiddle

    html 代码:(在您的服务器中更改 html 页面的 href="/")

    <span id='mycaption'></span>
    <br>
    <div id="mydiv" style="width:300px; height: 300px"></div>
    <br>
        <a class="iframe" href="/" caption="My title">link</a>
    

    jquery 代码:

    $(".iframe").on("click", function(e) {
            e.preventDefault();
            var mylink = $(this).attr('href');
            $("#mydiv").load(mylink);
    
            var mycap = $(this).attr('caption');
            $("#mycaption").html(mycap);
    }); 
    

    【讨论】:

    • 谢谢大家,我怎么没想到!呵呵。知道如何使用下一个和上一个按钮浏览链接(htmls)吗?我应该为此提出另一个问题吗?
    • @zefs 最好提出另一个问题,不要忘记先尝试自己解决它:) 也许您可以使用属性 data-*,使用 data-next 和 data-prev下一个/上一个 url 并使用 jquery 获取.. $('#next').data("next");。想想吧。
    【解决方案2】:

    假设您的 span 有一个 ID(让我们使用 test)

    $(".iframe").on("click", function(e) {
        e.preventDefault();
        var $mylink = $(this).attr('href');
        $mydiv.load($mylink)
    
        //Span stuff
        var caption = this.caption;
        $("#test").html(caption);
    

    });

    【讨论】:

      【解决方案3】:

      您的代码本身就有答案。在获得“href”时获得“标题”

      $("#mySpan").html( $(this).attr("caption") );
      

      【讨论】:

        猜你喜欢
        • 2021-11-13
        • 1970-01-01
        • 1970-01-01
        • 2015-01-31
        • 1970-01-01
        • 2014-12-07
        • 2016-05-28
        • 1970-01-01
        • 2020-04-27
        相关资源
        最近更新 更多