Any difference between .innerHTML and .set('html','') in mootools?

耗尽温柔 提交于 2019-12-05 01:34:09

问题


To set the html of elements on my site, I use mostly

$('elementId').innerHTML = "<p>text</p>";

Looking through the mootools docs, I found this example given:

$('myElement').set('html', '<div></div><p></p>');

Is there any difference between these? Should I go through and change .innerHTML to the mootools method, or doesn't it make a difference?


回答1:


the reason why the first one works is because - as it stands - a $ selector (document.id) in mootools returns the actual element. this - in normal browsers - is identical to document.getElementById() and the element object exposes any and all of its attributes/properties for you to edit.

the problems with NOT using .set are:

  • when mootools 2.0 aka MILK gets released, it won't work as it will be wrapped like jQuery and the selector won't return the object (mootools is becoming AMD hence it won't modify native Types - Element, Array, Number, String, Function(maybe!) - prototypes).
  • you cannot chain this. with set you can: $('someid').set("html", "loading...").highlight();, for example.
  • set is overloaded - it can set either a single property or multiples by means of passing an object. eg, element.set({html: "hello", href: "#", events: boundObj});
  • look at https://github.com/mootools/mootools-core/blob/master/Source/Element/Element.js#L936-942 - you can pass an array as an argument and it will join it for you, this makes it easy to work with multi-line strings and ensures performance in IE

edit: the BBT fan has kind of opened a separate topic: should the framework try to block you / prevent you from doing things that break the browser?

  • if you want to, you can add disallowed elements by changing that setter Element.Properties.html.set = function() { var tag = this.get("tag"); ... check tag }; - isn't mootools great?

mootools - by default - will NOT try to prevent you from doing stupid shit [tm] - that's your responsibility :) try setting height on an element to a negative value in IE, for example. should the Fx class prevent you from doing that? No. Should the setter prevent you? No. The footprint of constant checks to see if you are not breaking means it will slow everything down in performance-critical cases like animations.



来源:https://stackoverflow.com/questions/9115104/any-difference-between-innerhtml-and-sethtml-in-mootools

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!