我有以下代码,它以 html 列表格式动态生成目录树。当我尝试使用 javascript 操作列表项以在项目末尾添加“+”时,它不起作用。我知道 jquery 是正确的,我在同一服务器的另一个页面上使用过它。jquery 是否无法处理使用 asp.net 动态生成的服务器端的数据?
<script langauge="C#" runat="server">
string output;
protected void Page_Load(object sender, EventArgs e) {
getDirectoryTree(Request.QueryString["path"]);
itemWrapper.InnerHtml = output;
}
private void getDirectoryTree(string dirPath) {
try {
System.IO.DirectoryInfo rootDirectory = new System.IO.DirectoryInfo(dirPath);
foreach (System.IO.DirectoryInfo subDirectory in rootDirectory.GetDirectories()) {
output = output + "<ul><li>" + subDirectory.Name + "</li>";
getDirectoryTree(subDirectory.FullName);
if (subDirectory.GetFiles().Length != 0) {
output = output + "<ul>";
foreach (System.IO.FileInfo file in subDirectory.GetFiles()) {
output = output + "<li><a href='" + file.FullName + "'>" + file.Name + "</a></li>";
}
}
output = output + "</ul>";
}
} catch (System.UnauthroizedAccessException) {
//This throws when we don't have access, do nothing and move one.
}
}
</script>
然后我尝试使用以下内容操作输出:
<script langauge="javascript">
$('li > ul').not('li > ul > li > ul').prev().append('+');
</script>
仅供参考,div的代码如下:
<div id="itemWrapper" runat="server">
</div>