[英]jQuery change link href with .load()
我正在尝试制作一个类似保管箱的自定义上下文菜单。 我需要的是,当用户右键单击某个元素时,上下文菜单中弹出的诸如“编辑”和“删除”之类的选项链接到该特定元素的“编辑”和“删除” 。 上下文菜单:
<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的href的地方,则在if语句中看到引号特定于右键单击的元素的变量。
我知道如何将.load()用于div-$(“#ID”)。load(...)将加载该div中回显的内容。 但是,如何在链接的href内部加载(a)呢?
非常感谢。 非常感谢您的帮助。
如果我理解正确,并且想要更改标签的href,则要使用.attr()函数
所以看起来像这样
$("a").attr("href", "http://newlink.com")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.