jQuery-单击链接时将光标放在输入字段中 [英] jQuery - Place cursor in input field when link clicked

查看:51
本文介绍了jQuery-单击链接时将光标放在输入字段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在单击特定链接后,我想将光标放置在输入字段中.我正在将其用于小型搜索引擎.

I would like to place the cursor in an input field after a specific link is clicked. I'm using this for a small search engine.

想象一个输入字段:[].然后,一些链接在输入字段中添加了一个字符串,例如:丹麦,英国等.

Imagine an input field: []. Then some links which is adding a string like: Denmark, England etc. to the input field.

现在,我需要像这样放置光标:丹麦[在这里]".

Now I need the cursor to be placed like this: "Denmark, [here]".

这可能吗?

*更新*

我现在正在使用此代码替换文本,您将如何在其中添加位置光标"技巧?

I'm using this code right now to replace the text, how would you add the "position cursor"-trick with this?

$('#denmark').click(function(){     
   $('#searchBoxBig').val('Denmark, ');
});

推荐答案

您可以使用selectionStartselectionEnd属性设置光标在输入框中的位置.如果您不想选择任何文本,只需使它们彼此相等即可.

You can set where the cursor is in an input box with the selectionStart and selectionEnd properties. If you don't want any text to be selected, just make them equal to each other.

这是您可能的操作方式:

Here's how you might do it:

var input = $("input");
input[0].selectionStart = input[0].selectionEnd = input.val().length;

这篇关于jQuery-单击链接时将光标放在输入字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆