ITeye博客

Chrome、Safari下焦点事件中的select()自动全选

Pweb · 2010-10-23 18:12 · Javascript · Web前端 · Markdown

JS焦点事件自动全选方法:

 

 

<input onfocus="onfocusFn(this);" value="text here" />

 

function onfocusFn(tag) {
	tag.select();
}

 

以上代码在Firefox或IE上成功实现,但如果是Chrome或Safari,则需要增加onmouseup事件阻止浏览器的默认事件:

 

<input onfocus="onfocusFn(this);" onmouseup="onmouseupFn(event);" value="text here" />

 

function onmouseupFn(eventTag) {
	var event = eventTag||window.event;  
	event.preventDefault();
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。