csQuery从被操纵的dom中选择元素?
问题描述:
我从一个很好的文件渲染dom。 然后我通过点击事件来操纵jQuery。csQuery从被操纵的dom中选择元素?
我的问题是: 如何从现在的代码中获取操纵元素?
$(document).ready(function() {
var x = $(".class");
x.on("click", function() {
$(this).addClass("editable");
});
});
public static string filePath = HttpContext.Current.Request.PhysicalApplicationPath;
public static string file = filePath + "/templates/index.html";
public CQ dom = CQ.CreateFromFile(file);
protected void Page_Load(object sender, EventArgs e)
{
var html = dom.Render();
if (!IsPostBack)
{
Response.Write(html);
}
}
protected void btnSave_OnClick(object sender, EventArgs e)
{
var editable = dom.Select(".simplecms.editable").Text();
// Obviously this string will contain the value from the original dom, how can I retrieve the manipulated dom here?
}
答
一旦代码是活在浏览器中,它将不再(必然)反映在文件中的结构。即使在你操纵jQuery中的任何东西之前,浏览器偶尔也会为其规范化目标添加额外的标签以达到规范化目的。因此,您不应该期望能够从文件系统中检索它。
这个问题的标准答案就像这个问题一样,归结为“在运行时我如何从我的网页获取数据(任何种类)到我的服务器?是在你的服务器上实现一个REST接口,并使用AJAX发送你需要的任何数据给服务器。 jQuery有a great AJAX interface,你可以很容易地做一些像$(body).html()
或$('.some#selector').html()
这样的字符串表示来发送到你的服务器。棘手的部分,取决于您的服务器设置,可能会设置REST界面,但这有点超出了这个问题的范围。从REST端点收到字符串后,您应该可以使用CQ.Create(htmlString)
为CsQuery生成您的DOM。
感谢您的回复 - 不是我希望的答案。我相信有一个更简单的方法来解决我的问题:)我会继续调查。 – MartinDK81