我具有由一个核心模块生成链路内(意味着我不能修改的代码)作为这样的:参考ID(如选择器),其是已经HTML链接标记
<a id="my-unique-id-1" class="my-link-class" href="/switch off">Switch off</a>
问题是,ID和类位于<a>
标记内,我没有任何可用的元素缠绕在我可以使用的链接上。
单击时,它会做什么,它做服务器端(见下面的代码),然后返回此:
<a id="my-unique-id-1" class="my-link-class it-is-off" href="/switch on">Switch on</a>
我想更换或修改完成第一个链接。
首先,jQuery脚本:
$(".my-link-class").click(function() {
var current_id = $(this).attr('id');
var link = $(this).attr('href');
$.ajax({url: link, success: function (result) {
//All works fine up to here. The changes are made in server side and returns the new link as the result.
//Following is my problem:
if(result){
$(current_id).replaceWith(result); //the selector is wrong, I know.
}
}
}
我的问题是,ID(current_id)已经是<a>
标签内。 如何参考标签中的选择器。 我想:
$(current_id).replaceWith(result); //nothing happens
$('#' + current_id).replaceWith(result);
$('a#' + current_id).replaceWith(result);
但我得到的最后两个类型错误:Node.appendChild的参数1未实现接口节点。 (我知道我可以做的其他事情比replaceWith如改变链接中的文本和href,但这里的问题是首先找到选择器)。
'$(a.my-link-class)'在句法上不正确。那是因为它是示例代码吗? –
你可以用他们的hrefs来获取它们,比如'$('[href =“/ switch off”]')' – Ted
Typo fixed $(“。my-link-class”) –