[英]Strange Javascript/JQuery behaviour when creating a div element
menuPaneToBe.title = document.createElement("div");
$(menuPaneToBe).append(menuPaneToBe.title);
console.log(1)
console.log(menuPaneToBe.title)
$(menuPaneToBe.title).addClass("menuTitle");
console.log(2)
$(menuPaneToBe.title).html("Title"); //Just placeholders, gets changed by circular-view
console.log(3)
Console 安慰
1 MenuPane.js:12
[object HTMLDivElement] MenuPane.js:13
Uncaught Error: Syntax error, unrecognized expression: [object HTMLDivElement] jquery-1.11.0.min.js:2
Why is the title just [object HTMLDivElement] instead of being a proper object? 为什么标题只是[object HTMLDivElement]而不是适当的对象? and why can't I set the class?
为什么我不能设置课程? MenuPaneToBe is a custom tag that is set to display block.
MenuPaneToBe是设置为显示块的自定义标签。
<menu-pane id="menu"></menu-pane>
menu-pane {
display: block;
height: 100%;
width: 30%;
border-left: 1px solid black;
}
It is an strange issue, if menuPaneToBe
is referring to a dom element(not jQuery object) then the title property will refer to the element's title property which is a string value. 这是一个奇怪的问题,如果
menuPaneToBe
引用的是dom元素(而不是jQuery对象),那么title属性将引用元素的title属性,后者是字符串值。 At least in Google Chrome when you assign a value to title of an element, it converts it to a string object. 至少在Google Chrome浏览器中,当您为元素的标题分配值时,它将其转换为字符串对象。
So when you say menuPaneToBe.title = document.createElement("div")
, it assigns the string value [object HTMLDivElement]
to menuPaneToBe.title
, which is an invalid jQuery selector. 因此,当您说
menuPaneToBe.title = document.createElement("div")
,它将字符串值[object HTMLDivElement]
分配给menuPaneToBe.title
,这是一个无效的jQuery选择器。
Then when you say $(menuPaneToBe.title)
it will throw the said error. 然后,当您说
$(menuPaneToBe.title)
,它将抛出该错误。
Just change the property name from title
to something else like title2
只需将属性名称从
title
更改为title2
其他title2
var menuPaneToBe = $('#menu')[0];
menuPaneToBe.title2 = document.createElement("div");
$(menuPaneToBe).append(menuPaneToBe.title2);
console.log(1)
console.log(menuPaneToBe.title2)
$(menuPaneToBe.title2).addClass("menuTitle");
console.log(2)
$(menuPaneToBe.title2).html("Title"); //Just placeholders, gets changed by circular-view
console.log(3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.