[英]Why textarea's .focus() and .blur() don't work
I have a textarea element in my form as follow. 我的表单中有一个textarea元素,如下所示。
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form id="advertiseForm" name="advertiseForm" method="post" >
<textarea rows="20" cols="70" name="textarea" id="textarea">Please enter additional information here...</textarea>
</form>
<script type="text/javascript" src="advertisementlandedvalidationatclient.js"></script>
</body>
Then I put .focus and .blur into a javascript file as 然后我将.focus和.blur放到javascript文件中
advertisementlandedvalidationatclient.js
$("#textarea")
.focus(function() {
if (txt == null) return;
if (txt.value == "Please enter additional information here...") txt.value = '';
})
.blur(function() {
if (txt == null) return;
if (txt.value == '') txt.value = "Please enter additional information here...";
});
When I click or remove the mouse from textarea, "Please enter additional information here..."
doesn't toggle (appear/disappear). 当我单击鼠标或从文本区域中删除鼠标时,
"Please enter additional information here..."
不会切换(显示/消失)。
I tried another approach in this link It works, that approach needs the function SetMsg
needs to be on top of the textarea
. 我在此链接中尝试了另一种方法,该方法有效,该方法需要功能
SetMsg
需要在textarea
之上。 I don't like that approach because I want to separate HTML and Javascript codes into separate files and because of some other elements I need 我不喜欢这种方法,因为我想将HTML和Javascript代码分离到单独的文件中,并且由于某些其他因素,我需要
<script type="text/javascript" src="advertisementlandedvalidationatclient.js"></script> below the form.
Why it doesn't work in the first .focus/.blur method and what could be the best approach? 为什么在第一个.focus / .blur方法中不起作用,什么是最佳方法? Thanks
谢谢
You can use placholder attribute for the input elements 您可以对输入元素使用placholder属性
<textarea rows="20" cols="70" name="textarea" id="textarea" placeholder="Please enter additional information here..."></textarea>
If you really want this to do this with jQuery, you may fix the code as.. 如果您确实希望使用jQuery做到这一点,则可以将代码修复为:
$("#textarea")
.focus(function() {
if ($("#textarea").val() == null) return;
if ($("#textarea").val() == "Please enter additional information here...") $("#textarea").val('');
})
.blur(function() {
if ($("#textarea").val() == null) return;
if ($("#textarea").val() == '') $("#textarea").val('Please enter additional information here...');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.