所以我正在尝试创建一个函数,设置/删除字段中的水印,我已经开始工作了,其中输入的 html 元素如下所示:
<input type="text" name="pollName" id="pollName" value="DD-MM-YYYY" onfocus="javascript: watermark(this,true,'DD-MM-YYYY')" onblur="javascript: watermark(this,false,'DD-MM-YYYY')" >
然而,这很长,我正在尝试将代码移动到 javascript,我打算在其中使用此代码:
function watermark(dom,isFocus,watermark)
{
if(isFocus)
{
if(dom.value === watermark)
{
dom.value = "";
}
}
else if(!isFocus)
{
if(dom.value === '')
{
dom.value = watermark;
}
}
}
document.ready(function(){
$('#addOption').click(addOption);
$('.dateSelector').focusin(watermark(this,1,'DD-MM-YYYY'));
$('.dateSelector').focusout(watermark(this,0,'DD-MM-YYYY'));
});
这是有问题的HTML。
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>SPS: Create a new poll</title>
<link href="styles/stylePollCreator.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript" src="js/jsPollCretor.js"></script>
</head>
<body>
<div id="pollSetup">
<form action="" method="post">
<label for="pollName:">Poll name</label>
<input type="text" name="pollName" id="pollName" />
<br />
<label for="startDate">Start date: </label>
<br />
<input type="text" id="startDate" name="startDate" class="dateSelector" value="DD-MM-YYYY"/>
<label for="endDate">End date: </label>
<input type="text" id="endDate" name="endDate" class="dateSelector" value="DD-MM-YYYY"/>
<br />
<div id="pollOptions">
</div>
<input id="submitNewPoll" value="Create poll" type="submit" />
</form>
<button id="addOption">Add option</button>
</div>
</body>
</html>
addOption 函数工作正常,但是水印函数似乎在站点首次加载时调用一次,然后不再调用:/
感谢您的时间 :)