最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Google Chrome breaks when onfocus sets select size - Stack Overflow

matteradmin7PV0评论

The following javascript to resize a select list breaks in Google Chrome. It works when tabbing into the field, but clicking on it results in the "Aw, Snap!" error page.

<select onfocus="this.setAttribute('size', 3);">
<option>selectList with onfocus</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>

Works fine in FF and IE. It's some kind of conflict between onfocus (there's no problems if I implement it onClick) and setting the size attribute. I'm told it breaks in Safari too.

Any assistance, ideas or workarounds are greatly appreciated.

(P.S. Yeh I know it's not very nice form to resize a select list, but it's what the boss/client wants)

The following javascript to resize a select list breaks in Google Chrome. It works when tabbing into the field, but clicking on it results in the "Aw, Snap!" error page.

<select onfocus="this.setAttribute('size', 3);">
<option>selectList with onfocus</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>

Works fine in FF and IE. It's some kind of conflict between onfocus (there's no problems if I implement it onClick) and setting the size attribute. I'm told it breaks in Safari too.

Any assistance, ideas or workarounds are greatly appreciated.

(P.S. Yeh I know it's not very nice form to resize a select list, but it's what the boss/client wants)

Share Improve this question asked Apr 2, 2009 at 10:37 Sam WesselSam Wessel 8,8588 gold badges42 silver badges44 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

Change the line with the select to this:

<select onfocus="var that = this; setTimeout(function() {that.setAttribute('size', 3);}, 0);">

It works for me in Chrome. I haven't tried in Safari but I suspect it will work as well. Basically all we do here is escaping the call stack with setTimeout, which appears to get around the bug in Webkit.

I also found this:

  • http://code.google./p/chromium/issues/detail?id=4579
  • http://bugs.webkit/show_bug.cgi?id=17648

So it seems this is a WebKit issue and for some reason they don't want to fix it (make it standards-pliant).

As far as I can tell, Google Chrome ignores ALL select size attributes for multiple select boxes.

See link:

http://www.w3schools./TAGS/tryit.asp?filename=tryhtml_select_size

Use any other browser, then try Google Chrome. Just basic HTML attributes pletely ignored by Chrome.

Post a comment

comment list (0)

  1. No comments so far