gpt4 book ai didi

javascript - 如何通过JQuery获取html元素(PartialView)的祖先元素?

转载 作者:行者123 更新时间:2023-12-02 15:45:21 26 4
gpt4 key购买 nike

我想获取标签的祖先标签。位于查看,删除位于部分 View 。

我可以通过 AJAX 将部分 View 加载到 View 中。但是,我无法采用部分 View 的祖先来操作部分 View ,在我的例子中,删除部分 View 。

我的部分 View 是:

<img id="hello" src="@Url.Content("/content/images/" + @Model.Address)" />
<br />
<a id="linkToDel" onclick="clickme(this.id)">Delete</a>

View 是:

<div id="imageDivFirst"></div>

加载部分 View 的 JavaScript:

$('#imageDivFirst').load('/Home/ImagePartialView', { address: data.result.name });

我试图获得部分 View 的祖先:

function clickme(id) {
var a = document.getElementById("linkToDel").previousElementSibling.previousSibling.previousSibling;// not detected an ancestor
}
//then I want to take an ancestor of <a id="linkToDel"/>
// and delete the partial view

如何让这个祖先位于 View :

<div id="imageDivFirst"></div>//How to get this ancestor located at View?

最佳答案

您可以传递元素引用

<a id="linkToDel" onclick="clickme(this)">Delete</a> 

然后

function clickme(el) {
var a = el.parentNode;//parent element
$(el).parent().remove();//using jQuery to remove the parent element
$(el).parent().empty();//to remove the partial contents only, but the parent element will remain
}

关于javascript - 如何通过JQuery获取html元素(PartialView)的祖先元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32241012/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com