问题描述
经过大量搜索,我找到了以下主题:
After a lot of search I found the following threads:
很遗憾没有发布完整的表单嵌入代码或给出一个真实的例子。现在我只是不知道如何将nemisj的代码(在第一个链接上)或Mark的代码(在第二个链接上)包含在我的表单中:
Unfortunately in none of the posts a complete form embed code or a real example is given. Now I just don't know how to include nemisj's code (on the first link) or Mark's code (on the second link) into my form:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>
<script>
$(document).ready(function(){
$("#site").focus(function(){
if( this.value == this.defaultValue ) {
$(this).val("http://");
}
});
});
</script>
</head>
<body>
<form action="#" method="post">
<input id="name" type="text" name="name" value="Name" /><br />
<input id="site" type="text" name="mail" value="Website" /><br />
<input type="submit" value="Send">
</form>
</body>
</html>
我想知道是否有人可以帮助我,因为我被困住了!
I wonder if someone could kindly help me with this as I'm badly stuck!
非常感谢提前!
这是编辑过的代码,但它仍然不起作用:
Here's the edited code, but it still doesn't work:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>
<script>
function setCursor(node,pos){
var node = (typeof node == "string" || node instanceof String) ? document.getElementById(node) : node;
if(!node){
return false;
}else if(node.createTextRange){
var textRange = node.createTextRange();
textRange.collapse(true);
textRange.moveEnd(pos);
textRange.moveStart(pos);
textRange.select();
return true;
}else if(node.setSelectionRange){
node.setSelectionRange(pos,pos);
return true;
}
return false;
}
$(document).ready(function(){
$("#site").focus(function(){
if( this.value == this.defaultValue ) {
$(this).val("http://");
var node = $(this).get(0);
setCursor(node,node.value.length);
}
});
});
</script>
</head>
<body>
<form action="#" method="post">
<input id="name" type="text" name="name" value="Name" /><br />
<input id="site" type="text" name="mail" value="Website" /><br />
<input type="submit" value="Send">
</form>
</body>
</html>
推荐答案
在< script>里面;< / script>
标记是您的JavaScript所在的位置(虽然我们更喜欢将其放在单独的文件中,因此HTML页面本身不会存在任何JavaScript)。
Inside your <script></script>
tag is where your JavaScript goes (although we prefer putting it in a separate file, so that no JavaScript lives on the HTML page itself).
在其中,您可以调用 $(document).ready()
,它会传递函数(){ ......}
。在该函数内部是文档加载后将执行的所有代码。
Inside that, you have a call to $(document).ready()
, which passes a function() { ... }
. Inside that function is all the code that will be executed when your document has loaded.
在 内 为您提供服务打电话给 $('#site')。focus()
这本身就提供了一个函数 - 这次只要就会被调用#网站
元素获得焦点。并且可能是你想要改变光标位置的地方。
Inside that function you have a call to $('#site').focus()
which itself provides a function — this time one that will be called whenever the #site
element gains focus. And presumably that's where you want to change the cursor position.
所以,从,你可以把它放在你的任何地方< script>< / script>
然后在你的最里面的函数中你可以写:
So, taking the setCursor function from Set cursor at a length of 14 onfocus of a textbox you can put that anywhere in your <script></script>
and then inside that innermost function of yours you can write:
if( this.value == this.defaultValue ) {
$(this).val("http://");
var node = $(this).get(0);
setCursor(node,node.value.length);
}
这篇关于在输入文本字段中设置光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!