官术网_书友最值得收藏!

Injecting Hello World into an HTML DIV

Let us add an element to the HTML Document Object Model (DOM).

Getting ready

There is no oldie that is more goodie than greeting the world around us. There is an elemental power to our first Hello World, a power that proclaims, "We have the power to code syntactically correct. We have the power to change things on the screen. We have the power to accomplish business goals using this language. And, we are friendly and outgoing; 'Hello'."

How to do it...

Here is an example that demonstrates MooTools' syntax and power:

<script type="text/javascript" src="mootools-1.3.0.js"></script>
</head>
<body>
<div id="mycanvas">
Knock Knock, Who's there?
Hello, who?
</div>
<script type="text/javascript">
var whocanitbenow = 'Hello World!';
var readymsg = 'Okay to make the magic happen?';
if (confirm(readymsg,true,false)) {
// sexy part:
$('mycanvas').set('html',whocanitbenow);
} else {
// well, since they're being silly...
setTimeout(fallback_plan, 1000);
}
function fallback_plan() {
$('mycanvas').set( 'html', 'orange you glad there is a backup message?');
}
</script>

How it works...

So, as usual in our favorite MooTools cookbook, the alluring part is set with spacing above and below. Here is the 1, 2, 3 of what is happening in that nugget of goodness:

  • We use the MooTools dollar, $, to select our div
  • We use the MooTools method set()
  • We send two arguments to set()

    Note

    MooTools $ has a counterpart, the double dollar sign: $$, which is used for sending CSS selectors and returns a multiple element collection.

    The single $, originally reserved for grabbing DOM unique, id attributed elements takes no punctuation not already inherent in the name of the id attribute.

Using MooTools dollar not only empowers us with cross-browser compatible code to humbly abstract and replace the infamous, JavaScript built-in, getElementById() method to get the element, but it also enhances the returned object with Moo-methods like get() and set().

Tip

Using Multiple Frameworks

It is imperative to use document.id(<id>) instead of its alias, $(<id>), when working with multiple frameworks. Reviewing the Moo-source shows that document.id is used exclusively. When another framework is using the dollar syntax, MooTools attempts to sense that and not extend the object.

There's more...

The get() method returns the attribute or property value requested while the set() method takes the attribute/property argument and a value to which the property should change. In this example we could use $('mycanvas').set('text',whocanitbenow);. That would do the same thing as our example, since we have only altered text, but would prevent us from injecting HTML and would strip all existing HTML from our text.

We now see that our goal with this recipe is to try it out; be sure to change the whocanitbenow variable to something with HTML in it like this:

<script type="text/javascript">
var whocanitbenow = '<strong>Hello <em>World</em></strong>!';
var readymsg = 'Okay to make the magic happen?';
if (confirm(readymsg,true,false)) {
// "alluring" part:
$('mycanvas').set('html',whocanitbenow);
} else {
// well, since they're being silly...
setTimeout(fallback_plan, 1000);
}
function fallback_plan() {
$('mycanvas').set( 'html', 'orange you glad there is a backup message?');
}
</script>

See also

For more information on CSS selectors see http://www.w3.org/TR/CSS2/selector.html.

主站蜘蛛池模板: 安溪县| 方城县| 堆龙德庆县| 桐庐县| 龙井市| 靖边县| 罗定市| 岑巩县| 新余市| 阿拉尔市| 彭山县| 兖州市| 安新县| 驻马店市| 三江| 东至县| 双牌县| 金昌市| 南京市| 博乐市| 怀仁县| 淮安市| 德安县| 石狮市| 五河县| 博爱县| 武威市| 桃园县| 长岭县| 漳浦县| 防城港市| 甘泉县| 钟山县| 壤塘县| 喀什市| 鄂尔多斯市| 集安市| 绍兴县| 广德县| 漳州市| 鸡西市|