简体   繁体   English

jQuery:在HTML中使用来自属性的选择器

[英]jQuery: Using Selectors on HTML from an Attribute

I have some HTML that is stored as an attribute on a tag. 我有一些HTML作为属性存储在标签上。 I can access it in jQuery using 我可以使用jQuery访问它

$("input[id$='_myField_hiddenSpanData']").attr("value")

This looks like this: 看起来像这样:

"<span id='spantest\\user\u0026#39; tabindex='-1' contentEditable='false' class='ms-entity-resolved' title='test\\user\u0026#39;><div style='display:none;' id='divEntityData' key='test\\user\u0026#39; displaytext='Test User' isresolved='True' description='test\\user\u0026#39;><div data=''></div></div><span id='content' tabindex='-1' contenteditable onMouseDown='onMouseDownRw();' onContextMenu='onContextMenuSpnRw();' >Test User</span></span>"

I would need the value of the key attribute (test\\user). 我需要key属性的值(test \\ user)。 Can I somehow tell jQuery to parse a block of HTML and apply selectors to it? 我可以以某种方式告诉jQuery解析HTML块并对其应用选择器吗? I found I can wrap it into a new jQuery object by wrapping it into another $(): $($("input[id$='_myField_hiddenSpanData']").attr("value")) but I still did not manage to apply a selector on it. 我发现可以通过将其包装到另一个$()中来将其包装到新的jQuery对象中: $($("input[id$='_myField_hiddenSpanData']").attr("value"))但我仍然无法管理在其上应用选择器。

Any hints? 有什么提示吗? And no, sadly I do not control the markup that generates the hidden field. 不,不幸的是,我不控制生成隐藏字段的标记。

Wrap your crappy markup with a jQuery object, and then use the find function to apply a selector to it... 用jQuery对象包装糟糕的标记,然后使用find函数向其应用选择器...

var crappyHtml = $("input[id$='_myField_hiddenSpanData']").attr("value");
var key = $(crappyHtml).find("div[key]").attr("key");
alert(key);

You should be able to pass it as a context. 您应该能够将其作为上下文传递。 Does this work?: 这行得通吗?:

$('#divEntityData', $($("input[id$='_myField_hiddenSpanData']").attr("value"))).attr('key');

Try this: 尝试这个:

var html = $("input[id$='_myField_hiddenSpanData']").attr("value");
var user = $(html).find("#divEntityData").attr("key");
alert("user=" + user);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM