[英]jQuery doesn't see newly created object. How to refresh list of DOM objects?
In my app when user clicks on button, browser creates new object. 在我的应用程序中,当用户单击按钮时,浏览器会创建新对象。 For instance:
例如:
$("#div1 svg").append("<ellipse id='#obj0' class='svgobjects'>");
It works. 有用。 It appears in Chrome development tools.
它出现在Chrome开发工具中。 Then I want to set size of my ellipse using some data.
然后我想用一些数据设置椭圆的大小。
if (!$(ID).length > 0) {
console.log("Div doesn't exist");
} else {
console.log("I found it");
}
$(ID).attr("cx", data["startX"]).attr("cy", data["startY"]).attr("rx", this.rx).attr("ry", this.ry).attr("fill", data["color"]);
All of these variables are OK. 所有这些变量都可以。 All of them contains correct data.
所有这些都包含正确的数据。 But in console appears: "Div doesn't exist".
但在控制台出现:“Div不存在”。
The problem is: this div IS there and have the same id as "ID" variable ("#obj0"). 问题是:这个div在那里并且具有与“ID”变量(“#obj0”)相同的id。 But in spite of all jquery sais that div doesn't exist.
但是尽管所有的jquery sais都不存在div。
If you're selecting your ellipse
element using its ID, make sure to set it correctly with a valid value. 如果您使用其ID选择
ellipse
元素,请确保使用有效值正确设置它。
With CSS (and therefore with jQuery too) you would use #obj0
to select the element having the ID obj0
, but you don't have such element as what you have is an element with an ID defined to #obj0
, which is an invalid value. 使用CSS(因此也使用jQuery),您可以使用
#obj0
来选择ID为obj0
的元素,但是您没有这样的元素,因为您拥有的元素具有定义为#obj0
的ID,这是无效的值。
To solve your problem, replace the following: 要解决您的问题,请替换以下内容:
$("#div1 svg").append("<ellipse id='#obj0' class='svgobjects'>");
With this: 有了这个:
$("#div1 svg").append("<ellipse id='obj0' class='svgobjects'>");
Edit: 编辑:
In HTML5 the ID #obj0
is actually valid (see http://www.w3.org/TR/html-markup/global-attributes.html ), but in this case you will have to select it using $("#\\#obj0")
instead of $("#obj0")
. 在HTML5中,ID
#obj0
实际上是有效的(请参阅http://www.w3.org/TR/html-markup/global-attributes.html ),但在这种情况下,您必须使用$("#\\#obj0")
而不是$("#obj0")
。
IF you are using HTML5
如果您使用的是
HTML5
id="#obj0" is valid ,
then you have to select like $("#\\#obj0")
然后你必须选择像
$("#\\#obj0")
if you are not using HTML5
you should obj0
instead of '#obj0'
如果你不使用
HTML5
你应该obj0
而不是'#obj0'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.