[英]jQuery syntax not setting object property
My jQuery question I beleive is pretty simple, which is driving me insane that I can't get it. 我相信的jQuery问题非常简单,这使我发疯,我无法理解。
I have an object with a property "content", I want to be able to take that object, manipulate the property "content" with jQuery and then overwrite the value with the new value jQuery creates. 我有一个带有“内容”属性的对象,我希望能够使用该对象,用jQuery处理“内容”属性,然后用jQuery创建的新值覆盖该值。
Example: 例:
o.content = "<div><span>hello</span></div>";
$('div', o.content).addClass('test');
At this point I want o.content to be equal to <div class='test'><span>hello</span></div>
在这一点上,我希望o.content等于
<div class='test'><span>hello</span></div>
I can not for the life of me figure out the syntax. 我无法为自己的生活弄清楚语法。 Any help is really appreciated.
任何帮助都非常感谢。
Parse the html in o.content
, add the class, append the parsed html to a new <div>
, and get the html of the new div: 解析
o.content
的html,添加类,将解析的html附加到新的<div>
,并获取新div的html:
o.content = "<div><span>hello</span></div>";
var el = $(o.content).addClass('test');
o.content = $("<div>").append(el).html();
Edit: This assumes you want o.content
to still contain a string, rather than a jQuery object. 编辑:这假设您希望
o.content
仍然包含字符串,而不是jQuery对象。 In that case, it's simpler: 在这种情况下,它更简单:
o.content = $(o.content).addClass('test');
如果需要的话,这将为您提供一个字符串<div class="test"><span>hello</span></div>
:
$(o.content).addClass('test').wrap('<div>').parent().html();
I don't think you can lookup an element from a string like that.. I would rather do it like below, 我认为您无法从这样的字符串中查找元素。我宁愿像下面这样,
var content = "<span>hello</span>";
content = $('<div/>', {class: 'test'}).html(content)
DEMO: http://jsfiddle.net/k4e5z/ 演示: http : //jsfiddle.net/k4e5z/
from the docs of the jquery function , context
must be 从jquery函数的文档中 ,
context
必须是
A DOM Element, Document, or jQuery to use as context
用作上下文的DOM元素,文档或jQuery
Your context ( o.content
) is a string. 您的上下文(
o.content
)是一个字符串。 Also, the jQuery function is not able to select the entire context, it can only select elements in that context. 另外,jQuery函数无法选择整个上下文,它只能选择该上下文中的元素。
Try this instead: 尝试以下方法:
// make o.content a jquery element, not a string
o.content = $("<div><span>hello</span></div>");
// select on something inside the context (inside the div), not the div itself
$('span', o.content).addClass('test');
You want the following 您需要以下内容
o.content = "<div><span>hello</span></div>";
// Create a jQuery object you can call addClass on
var docFragment = $(o.content);
docFragment.addClass('test');
// Since there's no outerHTML in jQuery, append it to another node
var wrapper = $('div');
docFragment.appendTo(wrapper);
// The HTML of the wrapper is the outerHTML of docFragment
console.log(wrapper.html()); // outputs <div class='test'><span>hello</span></div>
Why not do it all in one line: 为什么不在一行中做所有事情:
var o = {};
o.content = $( "<div></div>" ) // create element
.addClass('test') // add class
.html( '<span>hello</span>' ); // append content
Fiddle: http://jsfiddle.net/kboucher/eQmar/ 小提琴: http : //jsfiddle.net/kboucher/eQmar/
o.content = $("<div><span>hello</span></div>");
o.content.addClass('test');
o.content is a jQuery object in this example, as opposed to just a string. o.content在此示例中是一个jQuery对象,而不是一个字符串。 Here's a demo on jsfiddle: http://jsfiddle.net/cvbsm/1/
这是jsfiddle上的演示: http : //jsfiddle.net/cvbsm/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.