【发布时间】:2012-07-23 03:35:53
【问题描述】:
在这里我创建了一个 jquery 函数,它获取 css-color 并创建具有 css-color 背景的元素:Edit my jsFiddle
html
<div ID="wrapper">
<div ID="addColor">
<input type="text" ID="hex">
<div ID="color">Your color</div>
<button ID="add">Add color</button>
<div CLASS="clear"></div> <!-- Clear float -->
</div>
<div ID="wrapGallery">
<h1>My Color Gallery</h1>
<ul ID="gallery"></ul>
</div>
</div>
js/jquery
$(function() {
//float left with some margin
$('#addColor')
.children().not('#add, .clear').css({
'float':'left',
'margin-right': '5px'
});
//Showing color on keyup
$('#hex').keyup(function() {
var hexCode = $(this).val();
$('#color').css('background-color', hexCode);
if ( hexCode !== '') {
$('#color').text('');
}else{
$('#color').text('Your color');
}
});
//Adding colors
$gallery = $('#gallery');
$('#add').click(function() {
var storedHex = $('#hex').val();
//check if empty
if (storedHex == '') {
alert('Enter something');
}
else {
//adding li
$("<li>").css('background-color', storedHex)
.hover(
function () {
$(this).text(storedHex);
},
function () {
$(this).text('');
})
.appendTo($gallery);
}
});
});
我唯一需要做的就是将创建的元素永久保存在文件中,以便我可以随时访问。我不知道该怎么做。
【问题讨论】:
-
将它们永久保存到什么文件?
-
@DavidStetler 我尝试使用表单传递值并回显该变量,但您知道如何将该新变量永久保存在同一个文件或任何其他方式上吗?
标签: php javascript jquery ajax json