JavaScript Tutorial/jQuery/merge
Версия от 18:52, 25 мая 2010; (обсуждение)
Merge array
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
alert($.merge( [0,1,2], [2,3,4] ));
});
</script>
</head>
<body>
<body>
<p></p><p></p><p></p>
</body>
</html>
Merge array and get the unique
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(
function() {
var $p = $("p").get();
var $before = "Before:\n";
$($p).each(
function() {
$before += this.id + "\n";
}
);
$p = $p.concat(
$("#my1, #my2").get()
);
var $merged= "\nMerged:\n";
$($p).each(
function() {
$merged += this.id + "\n";
}
);
$p = $.unique($p);
var $after = "\nAfter\n";
$($p).each(
function() {
$after += this.id + "\n";
}
);
alert(
$before + $merged + $after
);
}
);
</script>
</head>
<body>
<p id="my1">
</p>
<p id="my2">
</p>
<p id="my3">
</p>
</body>
</html>
Merge two arrays
<html>
<head>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(
function() {
var $vegetables = [
"C",
"T",
"L"
];
var $fruits = [
"C",
"A",
"C"
];
$merged = $.merge($vegetables, $fruits);
var $string = "";
$($merged).each(
function() {
$string += this + "\n";
}
);
alert($string);
}
);
</script>
</head>
<body>
</body>
</html>