我可以Elements使用抓取或注入或其他任何内容添加另一个DOM元素内的对象吗?
对象中有两个项目,都是通过Javascript创建的Element类型:
var firstElem = new Element("div", {text: "something"}); // <div>something</div>
var secondElem = new Element("div", {text: "else"}); // <div>else</div>
var myDivs = new Elements([firstElem, secondElem]);
Run Code Online (Sandbox Code Playgroud)
myDivs包含元素(firstElem,secondElem)作为数组,我想将此myDivs对象添加到下面的DOM元素,使用类似的东西$("container").grab(myDivs).所以添加前的DOM状态如下:
<div id="container"></div>
Run Code Online (Sandbox Code Playgroud)
添加后,它看起来像:
<div id="container">
<div>something</div>
<div>else</div>
</div>
Run Code Online (Sandbox Code Playgroud)
但是在调用时我收到此错误$("container").grab(myDivs):
Uncaught Error: NOT_FOUND_ERR: DOM Exception 8
Run Code Online (Sandbox Code Playgroud)
我可以逐个将每个元素添加到容器中,但我想知道是否有一种方法可以Elements直接添加对象,因为我的解决方案的架构方式.
jQuery如何处理子选择器或者我错过了一些明显的错误?当孩子不是什么时,我无法让它工作*.
这是我正在运行的jQuery选择器:
$("#myTable > tr").each(function() {
// do somthing }
);
Run Code Online (Sandbox Code Playgroud)
表结构是:
<table id="myTable">
<tr>
<td><button>someButton</button></td>
<td><textarea>...</textarea></td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
没有元素与上面的选择器匹配#myTable > tr.但下面列出的两个选择器工作正常.
$("#myTable tr") // search all descendants for tr
Run Code Online (Sandbox Code Playgroud)
或使用通配符匹配孩子:
$("#myTable > *") // search all child elements
Run Code Online (Sandbox Code Playgroud)
关于这里可能出现什么问题的任何想法?
谢谢你的快速回答!不幸的是只能选择一个.
我在现有类上有一个类别,它为类添加了一个属性和一些方法.
@interface AClass (ACategory) {
NSString *aProperty;
}
@property (nonatomic, retain) NSString *aProperty;
@end
Run Code Online (Sandbox Code Playgroud)
在实现文件中,我想在取消分配对象时释放此属性.但是,如果我dealloc在这个类中声明,它将根据我的理解覆盖原始类的dealloc.aProperty当对象被释放时,释放它的正确方法是什么?
@implementation AClass (ACategory)
@synthesize aProperty;
- (void)dealloc {
[aProperty release];
// this will skip the original dealloc method from what I understand
[super dealloc];
}
@end
Run Code Online (Sandbox Code Playgroud) 我正在阅读一篇文章(JavaScript Closures for Dummies),其中一个例子如下.
function buildList(list) {
var result = [];
for (var i = 0; i < list.length; i++) {
var item = 'item' + list[i];
result.push( function() {alert(item + ' ' + list[i])} );
}
return result;
}
function testList() {
var fnlist = buildList([1,2,3]);
// using j only to help prevent confusion - could use i
for (var j = 0; j < fnlist.length; j++) {
fnlist[j]();
}
}
testList();
Run Code Online (Sandbox Code Playgroud)
调用testList时,会出现一个警告框,其中显示"item3 undefined".文章有这样的解释:
当在行上调用匿名函数时,
fnlist[j]();它们都使用相同的单个闭包,并且它们在该闭包中使用i和item的当前值(其中我的值为3,因为循环已完成,并且item具有值'item3'). …
我已经看到在几个站点上引用了这个格式函数,但它们都没有一个如何将数字传入函数的明确示例.
我试过'12345'.format('0.00'),我相信它应该如何写,但它给了我一个错误,该对象不支持属性或方法.我也试过Number('12345').format('0.00'); var num ='12345'// num.format('0.00'); 格式('0.00','12345')甚至尝试使用数字而不是字符串12345.format(0.00).我错过了一些非常明显的东西吗?
包含的功能副本供参考,因此您无需访问该站点(填写所有缺失的部分).
/**
* I ? Google
*/
String.prototype.stripNonNumeric = function() {
var str = this + '';
var rgx = /^\d|\.|-$/;
var out = '';
for( var i = 0; i < str.length; i++ ) {
if( rgx.test( str.charAt(i) ) ) {
if( !( ( str.charAt(i) == '.' && out.indexOf( '.' ) != -1 ) ||
( str.charAt(i) == '-' && out.length != 0 ) ) ) {
out += str.charAt(i); …Run Code Online (Sandbox Code Playgroud) 我遍历在MooTools的数组,但使用速记通过数组进行遍历时,看到其他项目for..in环.当我使用常规for循环时,它工作正常.这是MooTools污染全局命名空间的问题还是我在这里做错了什么?
有一个createTabs()函数迭代数组并为数组中的每个值创建一个选项卡:
function createTabs() {
var myTabs = [["First", "a.png"], ["Second", "b.png"]];
for(var i in myTabs) {
var tab = new Tab(myTabs[i][0], myTabs[i][1]);
console.log(i);
}
}
Run Code Online (Sandbox Code Playgroud)
这是输出console.log(i):
0
1
$family
each
clean
associate
link
contains
extend
getLast
getRandom
include
combine
erase
empty
flatten
hexToRgb
rgbToHex
toJSON
Run Code Online (Sandbox Code Playgroud)
我理解前两个索引,但其余的来自哪里?
编辑:感谢Chetan和k Prime的快速回答.这是有道理的,Array.eachMooTools 的添加是更加清晰的迭代方式!
现在看起来好多了:
myTabs.each(function(item) {
var tab = new Tab(item[0], item[1]);
console.log(item);
});
Run Code Online (Sandbox Code Playgroud) 我在孩子存在的has_many关系上遇到验证时遇到问题,但父母却没有.但是,在创建/保存父对象时,我想确保已保存特定的子级(具有某些属性).
Parent对象有一个对象has_many Child.该Child对象持久化到数据库第一,因而不必父任何引用.关联结构是:
Parent
- has_many :children
Child
- someProperty: string
- belongs_to: parent
Run Code Online (Sandbox Code Playgroud)
例如,有三个子对象:
#1 {someProperty: "bookmark", parent: nil}
#2 {someProperty: "history", parent: nil }
#2 {someProperty: "window", parent: nil }
Run Code Online (Sandbox Code Playgroud)
仅当父项包含具有someProperty history和的子对象时,父项才有效window.
我在控制器中设置父节点:
p = Parent.new(params[:data])
for type in %w[bookmark_id history_id window_id]
if !params[type].blank?
p.children << Child.find(params[type])
end
end
// save the parent object p now
p.save!
Run Code Online (Sandbox Code Playgroud)
当子项被分配给父项时<<,它们不会立即保存,因为父项的ID不存在.为了保存父母,它必须至少有这2个孩子.我怎么能解决这个问题?欢迎任何输入.
如何向现有的onclick事件添加更多行为,例如,如果现有对象看起来像
<a href="http://abc" onclick="sayHello()">link</a>
<script>
function sayHello(){
alert('hello');
}
function sayGoodMorning(){
alert('Good Morning');
}
</script>
Run Code Online (Sandbox Code Playgroud)
我怎样才能为onclick添加更多行为,以及以下内容
alert("say hello again");
sayGoodMorning()
Run Code Online (Sandbox Code Playgroud)
最诚挚的问候,凯沙夫
我是iPhone应用程序编程的初学者.
我真的不喜欢我们设置起源和尺寸的方式,如:
UIView *view;
CGRect frame = view.frame;
frame.origin.x = 100;
view.frame = frame;
Run Code Online (Sandbox Code Playgroud)
要么:
UIView *view;
view.frame = CGRectMake(100, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
Run Code Online (Sandbox Code Playgroud)
所以我为UIView创建了一个类别:
@interface UIView (Origin)
-(void) setOriginX:(CGFloat)x;
-(void) setOriginY:(CGFloat)y;
-(void) setOriginX:(CGFloat)x y:(CGFloat)y;
-(void) setWidth:(CGFloat)w;
-(void) setHeight:(CGFloat)h;
-(void) setWidth:(CGFloat)w height:(CGFloat)h;
@end
@implementation UIView(Origin)
-(void) setOriginX:(CGFloat)x {
self.frame = CGRectMake(x, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
}
...
@end
Run Code Online (Sandbox Code Playgroud)
然后我可以写:
UIView *view;
[view setOriginX 100];
Run Code Online (Sandbox Code Playgroud)
这对我来说很方便,但是有什么顾虑我不应该做这样的事情,或者直接设置起源/尺寸的任何更简单的方法吗?
javascript ×5
mootools ×2
categories ×1
closures ×1
cocoa ×1
dealloc ×1
delegates ×1
dom ×1
for-in-loop ×1
formatting ×1
has-many ×1
html ×1
inheritance ×1
ios ×1
iphone ×1
jquery ×1
loops ×1
numbers ×1
objective-c ×1
onclick ×1
prototype ×1
uiview ×1
validation ×1