Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
显示选择选项时的JavaScript事件?_Javascript_Jquery_Html_Drop Down Menu - Fatal编程技术网

显示选择选项时的JavaScript事件?

显示选择选项时的JavaScript事件?,javascript,jquery,html,drop-down-menu,Javascript,Jquery,Html,Drop Down Menu,当元素显示/隐藏其列表时,会触发什么事件(如果有) 例如: <select> <option>1</option> <option>2</option> <option>3</option> </select> 提前谢谢 另外,我试图在元素显示时向其添加一个类,以便在浏览器显示“options popover”时显示与未显示时不同的自定义插入符号样式。这意味着select元素

元素显示/隐藏其
列表时,会触发什么事件(如果有)

例如:

<select>
    <option>1</option>
    <option>2</option>
    <option>3</option>
</select>
提前谢谢


另外,我试图在
元素显示
时向其添加一个类,以便在浏览器显示“options popover”时显示与未显示时不同的自定义插入符号样式。

这意味着select元素处于焦点位置。所以在这个场景中,您不需要javascript。您可以使用css来满足这一需求

select:focus {
  color:red;
}
但是,如果您确实想使用jquery,它将如下所示:

$("select").focus(function(){
  console.log(" h1 ");
})
var x = document.getElementById("select");
x.addEventListener("focus", myFocusFunction, true);
x.addEventListener("blur", myBlurFunction, true);

function myFocusFunction() {
    document.getElementById("select").style.backgroundColor = "yellow"; 
}

function myBlurFunction() {
    document.getElementById("select").style.backgroundColor = ""; 
}
使用纯javascript,为select指定一个id=“select”,您可以执行如下操作:

$("select").focus(function(){
  console.log(" h1 ");
})
var x = document.getElementById("select");
x.addEventListener("focus", myFocusFunction, true);
x.addEventListener("blur", myBlurFunction, true);

function myFocusFunction() {
    document.getElementById("select").style.backgroundColor = "yellow"; 
}

function myBlurFunction() {
    document.getElementById("select").style.backgroundColor = ""; 
}

这意味着select元素处于焦点位置。所以在这个场景中,您不需要javascript。您可以使用css来满足这一需求

select:focus {
  color:red;
}
但是,如果您确实想使用jquery,它将如下所示:

$("select").focus(function(){
  console.log(" h1 ");
})
var x = document.getElementById("select");
x.addEventListener("focus", myFocusFunction, true);
x.addEventListener("blur", myBlurFunction, true);

function myFocusFunction() {
    document.getElementById("select").style.backgroundColor = "yellow"; 
}

function myBlurFunction() {
    document.getElementById("select").style.backgroundColor = ""; 
}
使用纯javascript,为select指定一个id=“select”,您可以执行如下操作:

$("select").focus(function(){
  console.log(" h1 ");
})
var x = document.getElementById("select");
x.addEventListener("focus", myFocusFunction, true);
x.addEventListener("blur", myBlurFunction, true);

function myFocusFunction() {
    document.getElementById("select").style.backgroundColor = "yellow"; 
}

function myBlurFunction() {
    document.getElementById("select").style.backgroundColor = ""; 
}

使用
jquery
on change
单击添加类和onchange删除类怎么样?鼠标事件处理程序是不够的,因为使用键盘可以在选择元素中设置tab键,然后按下alt-down或alt-up键来删除和收回列表。(关于您实际想要实现的目标,您为什么要这样做?用户期望标准控件(如选择元素)具有标准行为。)使用
jquery
on change
单击添加类和onchange删除类怎么样?鼠标事件处理程序是不够的,因为使用键盘可以在选择元素中设置tab键,然后按下alt-down或alt-up键来删除和收回列表。(关于您实际试图实现的目标,您为什么要这样做?用户期望标准控件(如“选择元素”)的标准行为。)问题询问列表下拉(或再次隐藏)时是否有事件,这与“选择元素”接收焦点的情况不同。@nnnnnn是的,您是正确的。这不是这个问题的正确答案。没有与显示选项的select元素相关联的javascript事件。@user300838如果您真的想在select元素打开时实现向其添加类的预期效果,您可能需要使用jquery插件,例如,或者询问列表下拉时是否有事件(或再次隐藏),这与接收焦点的select元素不同。@nnnnnnnn是的,您是正确的。这不是问题的正确答案。没有与显示其选项的select元素相关联的javascript事件。@user300838,如果您真的希望在select元素打开时能够达到向其添加类的预期效果,请使用它如果是打开的,您可能需要使用jquery插件,如