【问题标题】:jQuery change link href with .load()jQuery 用 .load() 改变链接href
【发布时间】:2013-09-09 22:27:43
【问题描述】:

我正在尝试制作一个类似于 Dropbox 的自定义上下文菜单。我需要的是,当用户右键单击一个元素时,上下文菜单中弹出的“编辑”和“删除”等选项链接到该特定元素的“编辑”和“删除”时间>。上下文菜单:

<div class="screenlock-transparent">
<div class="context-menu-wrapper">
    <ul class="context-menu">
        <a href="<?php echo $edit_url; ?>"><li><i class="icon-edit" style="margin-right: 10px;"></i>Edit</li></a>
        <a href="<?php echo $delete_url; ?>"><li><i class="icon-trash" style="margin-right: 10px;"></i>Delete</li></a>
    </ul>
</div>
</div>

我解决这个问题的方法是让用户 jQuery 在用户右键单击一个元素时触发一个事件:

<script type="text/javascript">
$(document).ready(function(){
    // Launch on-right-click on element
    $(".custom-context-menu").mousedown(function(){
        if(event.which == 3) {
            if ($(this).hasClass("ul-all-years-faculty")) { // If on the general faculty ul
                // Load external php here to build url string specific to the element
                // Replace current href value with the url string produced by the external php script
            } else if ($(this).hasClass("ul-year-specific-faculty")) {
            } else if ($(this).hasClass("ul-semester")) {
            } else if ($(this).hasClass("ul-subject")) {
            }
        }
    });
});
</script>

如果 if 语句是我尝试编写将调用我的外部 php 脚本的代码并将上下文菜单中的“编辑”链接的 href 替换为 href包含特定于被右键单击的元素的变量的 url。

我知道如何将 .load() 用于 div - $("#ID").load(...) 将加载该 div 内的回显内容。但是,在链接 (a) 的 href 中加载呢?

非常感谢。非常感谢您的帮助。

【问题讨论】:

    标签: php jquery html ajax


    【解决方案1】:

    如果我理解正确,并且你想更改 a 标签的 href,你想使用 .attr() 函数

    http://api.jquery.com/attr/

    所以它看起来像这样

    $("a").attr("href", "http://newlink.com")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      • 1970-01-01
      相关资源
      最近更新 更多