Javascript jQuery:如何设置一个;a「;带preventdefault的标签和不带preventdefault的标签?

Javascript jQuery:如何设置一个;a「;带preventdefault的标签和不带preventdefault的标签?,javascript,jquery,Javascript,Jquery,我有一个链接围绕着另一个链接,如下所示: <a href="#" class="video-link"> <a href="#" class="not-video-link">link</a> </a> $(".video-link").live("click", function (e) { e.preventDefault(); // Capture the link's HREF attribute in a varia

我有一个链接围绕着另一个链接,如下所示:

<a href="#" class="video-link">
  <a href="#" class="not-video-link">link</a>
</a>
$(".video-link").live("click", function (e) {
    e.preventDefault();
    // Capture the link's HREF attribute in a variable. 
    var youtubeLink = $(this).attr('href');
    var youtubeDiv = $(this).attr('id');

    // Set the HTML (the content) of the "video destination div" 
    // to = your iframe, including the youtubeLink
    // that the user original saved into this node.

    $('#' + youtubeDiv + '').html('<iframe style="margin-left:-12px; margin-right:-12px;" frameborder="0" height="234" src="' + youtubeLink + '&autoplay=1" title="YouTube video player" width="418"></iframe>');
});
我现在已从div更改为div,在div标记内带有href attr。那是非法的吗

我找到了解决这个问题的办法

                $("a#not-video-link").click(function () {
                var youtubeLink1 = $(this).attr('href1');
                      window.open(youtubeLink1);
                      event.stopImmediatePropagation();

                    });

            $(".video-link").live("click", function(e) {
                    e.preventDefault();


                  // Capture the link's HREF attribute in a variable.
                  var youtubeLink = $(this).attr('href');
                  var youtubeDiv = $(this).attr('id');

                  // Set the HTML (the content) of the "video destination div" to = your iframe, including the youtubeLink that the user original saved into this node.
                  $('#' + youtubeDiv + '').html('<iframe style="margin-left:-12px; margin-right:-12px;" frameborder="0" height="234" src="' + youtubeLink + '&autoplay=1" title="YouTube video player" width="418"></iframe>');
                });
$(“非视频链接”)。单击(功能(){
var youtubeling1=$(this.attr('href1');
窗口。打开(youtubeLink1);
事件。stopImmediatePropagation();
});
$(“.video link”).live(“点击”,功能(e){
e、 预防默认值();
//在变量中捕获链接的HREF属性。
var youtubeLink=$(this.attr('href');
var youtubeDiv=$(this.attr('id');
//将“video destination div”的HTML(内容)设置为=您的iframe,包括用户原始保存到此节点中的youtubeLink。
$('#'+youtubeDiv+'').html('';
});

这会使链接停止另一个要启动的链接,反之亦然。

将它们保留为一个链接并在单击处理程序上执行所有处理如何

这样,您将拥有一个有效HTML和实现您想要的两个世界中的最好的一个。例如(根据我对该问题的理解):


使用jquery:

$(".video-link").live("click", function (e) {
    e.preventDefault();

    // Capture the link's HREF attribute in a variable. 
    var youtubeLink = $(this).attr('href');
    var youtubeDiv = $(this).attr('id');

    // Set the HTML (the content) of the "video destination div" 
    // to = your iframe, including the youtubeLink
    // that the user original saved into this node.

    $('#' + youtubeDiv + '').html('<iframe style="margin-left:-12px; margin-right:-12px;" frameborder="0" height="234" src="' + youtubeLink + '&autoplay=1" title="YouTube video player" width="418"></iframe>');

    // Here do whatever you want for the second link 
    var otherLink = $(this).data('otherHref');
});
$(“.video link”).live(“点击”,功能(e){
e、 预防默认值();
//在变量中捕获链接的HREF属性。
var youtubeLink=$(this.attr('href');
var youtubeDiv=$(this.attr('id');
//设置“视频目的地div”的HTML(内容)
//to=您的iframe,包括youtubeLink
//将用户的原始数据保存到此节点中。
$('#'+youtubeDiv+'').html('';
//这里做任何你想为第二个链接
var otherLink=$(this.data('otherHref');
});

将它们保留为一个链接,并在单击处理程序上执行所有处理如何

这样,您将拥有一个有效HTML和实现您想要的两个世界中的最好的一个。例如(根据我对该问题的理解):


使用jquery:

$(".video-link").live("click", function (e) {
    e.preventDefault();

    // Capture the link's HREF attribute in a variable. 
    var youtubeLink = $(this).attr('href');
    var youtubeDiv = $(this).attr('id');

    // Set the HTML (the content) of the "video destination div" 
    // to = your iframe, including the youtubeLink
    // that the user original saved into this node.

    $('#' + youtubeDiv + '').html('<iframe style="margin-left:-12px; margin-right:-12px;" frameborder="0" height="234" src="' + youtubeLink + '&autoplay=1" title="YouTube video player" width="418"></iframe>');

    // Here do whatever you want for the second link 
    var otherLink = $(this).data('otherHref');
});
$(“.video link”).live(“点击”,功能(e){
e、 预防默认值();
//在变量中捕获链接的HREF属性。
var youtubeLink=$(this.attr('href');
var youtubeDiv=$(this.attr('id');
//设置“视频目的地div”的HTML(内容)
//to=您的iframe,包括youtubeLink
//将用户的原始数据保存到此节点中。
$('#'+youtubeDiv+'').html('';
//这里做任何你想为第二个链接
var otherLink=$(this.data('otherHref');
});

不允许嵌套链接,仅将其视为无效构造

12.2.2嵌套链接是非法的

由A元素定义的链接和锚点不得嵌套;A 元素不能包含任何其他A元素

由于DTD将链接元素定义为空,因此链接元素可能是空的 也不能嵌套

由于这种情况,浏览器不会将它们呈现为嵌套的。。它只需在下面创建两个不同的链接(参见校对/图片)


证明:[单击检查并单击链接]

在不同的浏览器上,结构看起来也不同

注意:我不确定你在尝试什么。。但是正如flec提到的,你应该寻找其他的选择


@不,你没有。因为您使用的是preventDefault,所以可以将click事件附加到任何html标记


嵌套链接是不允许的,只是被认为是无效的构造

12.2.2嵌套链接是非法的

由A元素定义的链接和锚点不得嵌套;A 元素不能包含任何其他A元素

由于DTD将链接元素定义为空,因此链接元素可能是空的 也不能嵌套

由于这种情况,浏览器不会将它们呈现为嵌套的。。它只需在下面创建两个不同的链接(参见校对/图片)


证明:[单击检查并单击链接]

在不同的浏览器上,结构看起来也不同

注意:我不确定你在尝试什么。。但是正如flec提到的,你应该寻找其他的选择


@不,你没有。因为您使用的是preventDefault,所以可以将click事件附加到任何html标记


你不应该像那样嵌套链接。。它只是无效的HTML。我必须这么做,这是为了youtube iframe的创作。如果它不在“a”内,该函数将不起作用。@Thayer不,您不需要。因为您使用的是preventDefault,所以您可以将click事件附加到任何html标记上,即使您的html中有这样的标记。。浏览器不会这样渲染它。。它只会创建两个不同的链接。。你可以试试,你不应该像那样嵌套链接。。它只是无效的HTML。我必须这么做,这是为了youtube iframe的创作。如果它不在“a”内,该函数将不起作用。@Thayer不,您不需要。因为您使用的是preventDefault,所以您可以将click事件附加到任何html标记上,即使您的html中有这样的标记。。浏览器不会这样渲染它。。它只会创建两个不同的链接。。你可以试试
$(".video-link").live("click", function (e) {
    e.preventDefault();

    // Capture the link's HREF attribute in a variable. 
    var youtubeLink = $(this).attr('href');
    var youtubeDiv = $(this).attr('id');

    // Set the HTML (the content) of the "video destination div" 
    // to = your iframe, including the youtubeLink
    // that the user original saved into this node.

    $('#' + youtubeDiv + '').html('<iframe style="margin-left:-12px; margin-right:-12px;" frameborder="0" height="234" src="' + youtubeLink + '&autoplay=1" title="YouTube video player" width="418"></iframe>');

    // Here do whatever you want for the second link 
    var otherLink = $(this).data('otherHref');
});
<a href="#" class="video-link">
  <a href="#" class="not-video-link">link</a>
</a>