简体   繁体   English

查找距离最近的li的链接并在新页面中打开-Javascript

[英]Find link from closest li and open it in a new page - Javascript

I have a submenu divided in two parts:in the right side a li which contains a link and in the left side an icon for each li. 我有一个分为两部分的子菜单:右侧是一个包含链接的li,左侧是每个li的图标。

Icons use a css class,called 'submenubtn'.I want to make a javascript function which takes the link from the closest li,assign to that icon,and when that icon is clicked,the link should be open in a new tab. 图标使用css类,称为“ submenubtn”。我想制作一个JavaScript函数,该函数获取最接近的li的链接,分配给该图标,然后单击该图标,则该链接应在新选项卡中打开。

I hope I was clear enough,please ask me anything you didnt'n undertand. 我希望我足够清楚,请问我您是否不了解的任何内容。

here is the code i have until now: 这是我到目前为止的代码:

$(document).ready(function() {
    $("body").on("click", ".submenubtn", function() {
    var link = $(this).find("li").attr('href'); 
    //window.alert(link);
    window.open(link)
 });
 });

link returns "undifined". 链接返回“未定义”。

I don't know how much this will help,but the html page: 我不知道这有多大帮助,但是html页面:

<?Menu?>
<div id="<?$_name?>" class="atk-menu atk-menu-vertical atk-popover">
<ul>
    <?Item?>
        <?MenuItem?>
        <li id="<?$id?>" class="<?$class?>"> <a href="<?$href?>"><i class="<?$icon?>"></i><?label?>MenuItem<?/?></a></li>
        <?/MenuItem?>
    <?/?>
    <?$Content?>
</ul>
</div>
<?MenuSeparator?><?/MenuSeparator?>
<?/?>

EDIT I solved the problem..see in my answer the solution 编辑我解决了问题..请参阅我的答案

Use window.location.href = link; 使用window.location.href = link; and closest() function of JQuery to get what you want 和jQuery的最近()函数来获取所需的内容

To open it on a new window add window.open(link, '_blank') 要在新窗口中打开它,请添加window.open(link, '_blank')

i think using data tags is the solution 我认为使用数据标签是解决方案

here the soure: http://api.jquery.com/data/ 这里的来源: http ://api.jquery.com/data/

new li html: 新李HTML:

<li data-url="HERE THE URL!"/>

your new jQuery: 您的新jQuery:

$(document).ready(function() {
    $("body").on("click", ".submenubtn", function() {
    var link = $(this).closest('li').data('url'); 
    //window.alert(link);
    window.open(link)
 });
 });

li element cannot contain href attribute, you can give your li a data attribute such as: li元素不能包含href属性,您可以为li提供一个data属性,例如:

<li data-href="your url here"></li>

then you can use: 那么您可以使用:

$(document).ready(function () {
    $("body").on("click", ".submenubtn", function () {
        var link = $(this).closest('li').data('url');
        window.open(link, '_blank');
    });
});

find() used to find the descendants of your element which is not applicable in your case since your anchor is the child of your li element. find()用于查找元素的后代,由于您的锚点是li元素的子代,因此不适用于您的情况。

So you need to use closest() to traverse up the DOM tree and get the closest parent li instead. 因此,您需要使用closest()遍历DOM树并获取最接近的父级li


Try to use: 尝试使用:

 $(document).ready(function () { $("body").on("click", ".submenubtn", function () { var link = $(this).closest('li').find('a').attr('url'); window.location.href(link); }); }); 

Problem solved 问题解决了

I managed to solve this...it was pretty simple. 我设法解决了这个问题……这很简单。

$(document).ready(function() {
    $(".submenubtn").click(function() {
        a =   $(this).closest('a').attr('href');
        window.open(a);
        return false;
 });
 });

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

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