简体   繁体   English

打开选择使用javascript单击另一个元素

[英]Open select clicking another element with javascript

I've been looking for a way to open a select / combobox clicking another element. 我一直在寻找一种打开select / combobox点击另一个元素的方法。 I read answers saying that is impossible but... How is facebook doing it? 我读到的答案说这是不可能的但是...... facebook是如何做到的?

In your facebook timeline, when you want to change the date of a status, there is an ahchor and when you click it, a combobox appears. 在你的Facebook时间线中,当你想要更改状态的日期时,有一个ahchor,当你点击它时,会出现一个组合框。 I thought they had applied styles to their select so it would look like an anchor but it wasn't like that. 我认为他们已经将样式应用于他们的选择,所以它看起来像锚,但它不是那样的。

Their HTML code has an anchor and a select element, this last one is hidden. 他们的HTML代码有一个锚和一个选择元素,最后一个是隐藏的。 Then, I know the select element is positioned as absolute but I can't get from their javascript code how it works. 然后,我知道select元素定位为绝对,但我无法从他们的javascript代码中获取它是如何工作的。

I tried to simulate it like this 我试着像这样模拟它

$('#element').click();
document.getElementById('element').click();

None of this works. 这些都不起作用。 I saw a script that duplicated the SELECT element but it add 2 attributes to it: multiple and size. 我看到一个复制SELECT元素的脚本,但它为它添加了2个属性:multiple和size。 This changes the look of the select. 这会改变选择的外观。

I would like to know how it would be possible? 我想知道它怎么可能? I know maybe it won't work in IE < 9. 我知道也许它不会在IE <9中起作用。

We are talking about this? 我们在谈论这个?

<span class="period periodDay">
   <a class="uiIconText periodLabel" style="padding-left: 11px;"><img class="img" src="https://s-static.ak.facebook.com/rsrc.php/v1/yo/r/5-fr6z6ewzf.png" alt="" style="top: 4px;" width="6" height="6" />Tag hinzufügen</a>
   <select data-name="day" class="periodMenu dayMenu" name="backdated_date[day]">
      <option value="">Tag:</option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="2">more days...</option>
   </select>
</span>

The <a> tag is over the <select> and the click gets piped through. <a>标签位于<select>上方,点击通过管道传递。 If you click the link an the right end it does not work. 如果单击右端的链接则不起作用。

Update 更新

You can use this trick to open the select. 您可以使用此技巧打开选择。 Place the select below the anchor and move the anchor after the click. 将选择放在锚点下方,然后在单击后移动锚点。

<select id="moveme" style="position: relative;"
    onmousedown="$('#moveme').css('left', '100px');">
  <option value="">Tag:</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="2">more days...</option>
</select>​

Demo: http://jsfiddle.net/HfFWr/1/ 演示: http//jsfiddle.net/HfFWr/1/

Tested in Firefox, Chrome and IE. 在Firefox,Chrome和IE中测试过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM