改变元素的不透明度
<!DOCTYPE html>
<html>
<head>
<script>
function ChangeOpacity(x)
{
// 返回被选选项的文本
var opacity=x.options[x.selectedIndex].text;
var el=document.getElementById("p1");
if (el.style.opacity!==undefined)
{el.style.opacity=opacity;}
else
{alert("Your browser doesn't support this example!");}
}
</script>
</head>
<body>
<p id="p1">请从下面的例子中选择一个值,以改变此元素的不透明度。</p>
<select onchange="ChangeOpacity(this);" size="5">
<option />0
<option />0.2
<option />0.5
<option />0.8
<option selected="selected" />1
</select>
</body>
</html>
根据DIV内容 怎么得到DIV的ID
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script src="ec/jquery/jquery-1.6.4.min.js" type="text/javascript"></script> <script type="text/javascript"> function test(only) { var $objs = $("div[id]:contains('德国')"); var arr = []; $objs.each(function () { if (only && $(this).text().length > 2) return false; arr.push($(this).attr("id")); }); alert(arr); } </script> </head> <body> <div id="code1">中国</div> <div id="adeg">美国</div> <div id="dg3g">英国</div> <div id="gjr4">德国</div> <div id="bkjt">法国</div> <div id="Div1">德国1</div> <div id="Div2">德国2</div> <div id="Div3">德国3</div> <div id="Div4">德国4</div> <input type="button" value="取得包含德国的 div 的id" onclick="test(false)" /> <input type="button" value="取得只包含德国的 div 的id" onclick="test(true)" /> </body> </html>
each :
$("div").each(function() { if($(this).html()=="德国"){alert($(this).attr("id"))} });
$("div:contains('德国')").eq(0).attr("id");
创建数组对象
创建 Array 对象的语法: new Array(); new Array(size); new Array(element0, element1, ..., elementn);
for in
var x var mycars = new Array() mycars[0] = "Saab" mycars[1] = "Volvo" mycars[2] = "BMW" for (x in mycars) { document.write(mycars[x] + "<br />") }
合并两个数组
var arr = new Array(3) arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" var arr2 = new Array(3) arr2[0] = "James" arr2[1] = "Adrew" arr2[2] = "Martin" document.write(arr.concat(arr2))
用数组元素组成字符串join
var arr = new Array(3); arr[0] = "George" arr[1] = "John" arr[2] = "Thomas" document.write(arr.join()); document.write("<br />"); document.write(arr.join(".")); 输出结果: George,John,Thomas George.John.Thomas
数组排序 sort
document.write(arr.sort()) <script type="text/javascript"> function sortNumber(a, b) { return a - b } var arr = new Array(6) arr[0] = "10" arr[1] = "5" arr[2] = "40" arr[3] = "25" arr[4] = "1000" arr[5] = "1" document.write(arr + "<br />") document.write(arr.sort(sortNumber)) </script> 10,5,40,25,1000,1 1,5,10,25,40,1000
随机整数
下面的例子使用了 Math 对象的 floor() 方法和 random() 来返回一个介于 0 和 10 之间的随机数: document.write(Math.floor(Math.random()*11))