我将值从数组传递到锚点,然后在单击它们时需要运行一个函数。 功能
<script type="text/javascript">
function SetOption() {
var hash = window.location.hash.substring(1);
$('#uniform-id span').text(hash);//Changes the text in the dropdown box
$("#select id option:contains(" + hash + ")").attr('selected', 'selected');//Takes the hash and looks for an option that contains that hash
}
</script>
附加功能
$.each(color, function (index, value) {
var anchor=$('<a class="color">').css({
height: '30px',
width: '30px',
'background-color': value
}).attr({
"href": "#" + colorname[index], //append colorname as hash
"onclick": "SetOption();", //run function SetOption when clicked
});
$('#palette').append(anchor);
});
//var columns = $("#palette > a"); this part isn't important for the question.
//for(var i = 0; i < columns.length; i+=16) {
// columns.slice(i, i+16).wrapAll("<div class='column'></div>");
}
样本数组
var colorname = [];
colorname [ 0 ] = "color 1";
colorname [ 1 ] = "color 2";
colorname [ 2 ] = "color 3";
colorname [ 3 ] = "color 4";
var color = [];
color[ 0 ] = 'rgb(70,60,65)';
color[ 1 ] = 'rgb(95,58,61)';
color[ 2 ] = 'rgb(79,56,57)';
color[ 3 ] = 'rgb(87,50,65)';
目前该函数在可以为锚标签设置哈希之前运行,输出 html 如下所示:
<a class="color" href="#color 1" style="height: 30px; width: 30px; background-color: rgb(70, 60, 65);"></a>
似乎锚的 href 需要放在首位才能使其起作用,否则我的函数 SetOption 将需要延迟。