span
工作起来不像
input
所以简单的回答是你不能这么做。它甚至没有
focus
默认情况下为事件。
tabindex="-1"
使其可聚焦并使用
window.getSelection().selectAllChildren(this);
要选择其值:
$(".selectme").on('focus', function() {
window.getSelection().selectAllChildren(this);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class="selectme" tabindex="-1">this text should be selected by click</span>
Source
阿尔索
live
不赞成
,使用
on
相反。