focus光标未尾 或者开始

本文介绍了如何在HTML中通过JavaScript实现光标在文本框内的精准定位,包括光标置于文字末尾及文本框开始位置的操作,并详细解释了HTML5的占位符属性在提升用户体验上的应用。

focus捕获到焦点时,光标会出现在对应的文本框当中,在文本框中有值时,因不同需要我们会有两种情况需求:

1.光标出现在文字末尾

 

除了IE 浏览器不兼容之外,其他浏览器都可以支持。解决IE浏览器的兼容

   var tar=document.getElementByIdx_x("name");

   if(tar.attachEvent){

  tar.attachEvent('onmouseover',focus(tar),false);

  }else{

 tar.addEventListener('mouseover',focus(tar),false);

 }

function focus(tar){

    tar.focus();
    tar.value=tar.value;

}

 

先捕获焦点,把文框的值重新赋一下就OK 了。

2.光标出现在文本框的开始位置,输入文字时 默认的文字会消失。被新值而取代

在HTML5中会有属于placeholder

《一》

<input id="text2" placeholder="sssdfsfs" />

 

《二》

 

这里使用的是一个jquery插件

<!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=utf-8" />
<title>无标题文档</title>
<link rel="stylesheet" type="text/css" href="../csss/basic.css" />
<script language="javascript" type="text/javascript" src="../csss/jquery.js"></script>
<script language="javascript" type="text/javascript" src="../csss/jquery.infieldlabel.js"></script>
<style type="text/css">

.cr_mid {
    margin-top: 16px;
    width: 310px;
}

.cr_mid ul li {
    height: auto;
    position: relative;
    width: 310px;
}


.ep_con {
    background:url(../img/uc_input.png) no-repeat scroll 0 0 transparent;
    font-family: 'MuseoSans_500-webfont';
    height: 42px;
    position: relative;
    width: 312px;
}
label.deflut {
    -moz-user-select: none;
    color: #7794AA;
    display: inline-block;
    font-family: 'MuseoSans_500-webfont';
    font-size: 13px;
    left: 15px;
    position: absolute;
    top: 15px;
    transition: all 0.16s ease-in-out 0s;
    z-index: 2;
}

.input_ok {
    background: none repeat scroll 0 0 transparent;
    border: 0 none;
    color: #7794AA;
    font-family: 'MuseoSans_500-webfont';
    height: 20px;
    line-height: 20px;
    margin: 11px 12px;
    width: 280px;
}
</style>
</head>

<body>
<div class="cr_mid">
                <ul>                  
                    <li class="username_li">
                        <div class="email ep_con">
                        <label class="deflut inline-label" for="j_username" >Email address</label>
                        <input type="text" maxlength="200" tabindex="1" class="input_ok" name="j_username" id="j_username" autocomplete="off">
                        </div>
                    </li>
                    <li style="display:none;"></li>
                    <li class="ep_con password">
                  <div class="list_text_default">
                    <label class="watermark deflut inline-label" for="j_password" >Password</label>
                      <input type="password" tabindex="2" class="input_ok" name="j_password" id="j_password" style="" maxlength="200" autocomplete="off">
                  </div>               
          </li><li style="display: none;"><span for="j_password" generated="true" class="uc_red" style="display: none;">This field is required.</span></li>
                </ul>
                     <input type="submit" class="signin reg_com" value="Sign In">
                <div class="clear"></div>
                <h4 class="c_forget"><a href="/user/findPasswd.do">Forgot your password?</a></h4>
            </div>
           
            <script language="javascript" type="text/javascript">
                    
                     $(".inline-label").inFieldLabels();
            </script>
</body>
</html>

 

### UniApp 中实现文本框获取焦点的功能 在 UniApp 中,可以通过操作 DOM 或者调用内置 API 来实现文本框的聚焦功能。具体来说,可以利用 `ref` 属性绑定输入框组件,并通过 `$refs` 获取该组件实例,进而调用其方法来设置焦点。 以下是详细的实现方式: #### 使用 `uni.createSelectorQuery()` 方法 如果需要动态控制页面中的某个特定输入框获得焦点,可以借助 `uni.createSelectorQuery()` 查询节点并触发聚焦行为[^3]。 ```javascript methods: { focusInput() { uni.createSelectorQuery().select("#my-input").fields({ dataset: true, size: true, scrollOffset: true, properties: ["value"], context: true }).exec((res) => { res[0].node.focus(); }); } } ``` 在此代码片段中,`#my-input` 是目标输入框的选择器,需确保模板中有对应的 ID 定义。 #### 利用 `v-model` 和 `focus` 方法 另一种更简单的方式是直接使用 Vue 的 `ref` 特性配合原生的 `focus()` 函数完成相同效果[^4]。 ```html <template> <view> <input ref="textInput" v-model="textValue" placeholder="请输入..." /> <button @click="setFocus">点击聚焦</button> </view> </template> <script> export default { data() { return { textValue: "" }; }, methods: { setFocus() { this.$nextTick(() => { this.$refs.textInput.focus(); }); } } }; </script> ``` 这里定义了一个按钮用于触发光标定位逻辑;当用户按下此按键时会执行 `setFocus` 函数,在其中延迟处理以等待视图更新完毕后再尝试访问指定控件并激活它。 以上两种方案均可满足需求场景下的开发要求,请依据实际项目结构选取合适的方法实施即可。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值