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

Moving an element with a particular ID

Once an element is under our beck and call, we can send it somewhere else on the page.

How to do it...

In our HTML sketch, we are pretending to vote for the greater of two, very influential people. Each influence is represented by an element on the page with a unique ID attribute. Moving elements around when we know their unique ID attribute value is simple with the MooTool infused Element::inject() method.

<script type="text/javascript" src="mootools-1.3.0.js"></script>
</head>
<body>
<form action="javascript:" method="get">
Choose the greatest influence of the Internet:<br/>
<select id="influence">
<option value="">Vote Now:</option>
<option value="bill">Bill</option>
<option value="linus">Linus</option>
</select>
<input type="button" value="..."/>
</form>
<img id="bill" src="bill_gates.bmp" alt="Bill"/>
<img id="linus" src="linus_torvalds.bmp" alt="Linus"/>
<div>
<h1>Voted Greatest Influencer of the Internet: </h1>
</div>
<div id="my_target" style="width:150px; height:150px;
border:1px solid #BEBEBE; line-height:50px;
text-align:center;"></div>
<noscript>JavaScript is disabled.</noscript>
<script type="text/javascript">
$('influence').addEvent('change',function() {
var id_to_move = $('influence').value;
// use MooTools Element::inject() to move an item
$(id_to_move).inject('my_target');
// we can't have people voting twice
$('influence').removeEvents();
});
</script>
</body>
</html>

How it works...

The inject() method takes two parameters, though only one is mandatory. The requisite, first argument is the HTML element ID of the target canvas in which we wish to inject our element. The second argument that we may send is the keyword location of where we wish to inject the element relative to any existing content in the target element.

Appropriate values for the second argument of inject() include:

  • after
  • before
  • top
  • bottom (default value)

Two of the numerous methods that are similar to this are replace() and grab(). To remove all existing content, use the former. To reverse the inclusion syntax, use grab() thusly: $('my_target').grab('bill').

There's more...

Moving elements on a page is the cornerstone to providing a user-friendly environment. However, it is very important that we always do this with caution. Moving an element when the user is not expecting it can disorient them or even make them think the page is malfunctioning. Also, be sure to take those with sight disabilities into consideration. Not all screen-reading software will properly notify a non-sighted visitor that page elements have changed!

Note

The industry best practice for non-sighted visitors and page modifications on the fly: if the modification happens further down the page from the triggering element, screen reading software will not have read to the point of modification. It is okay to modify in this way.

Modifying the page above or prior to the triggering element is not okay without sending some sort of tactile feedback to the user that the page should be refreshed.

主站蜘蛛池模板: 高要市| 驻马店市| 合江县| 凯里市| 卓资县| 霍邱县| 桐柏县| 都昌县| 康平县| 邢台市| 西华县| 桂林市| 巴南区| 冀州市| 汉沽区| 康保县| 宜阳县| 吉首市| 小金县| 广丰县| 沁源县| 南雄市| 雷波县| 连城县| 义乌市| 惠来县| 濮阳县| 北川| 长海县| 五家渠市| 霍邱县| 珠海市| 大连市| 南阳市| 延边| 西乌珠穆沁旗| 怀远县| 康平县| 锡林郭勒盟| 西宁市| 崇州市|