Event.Target无法在Firefox中运行 - 修复后无法在Chrome中运行

时间:2016-07-11 14:58:48

标签: jquery firefox

我在Firefox中遇到常见问题,如果你没有将event.target作为参数传递,那么#itemCategories是未定义的。

请参阅以下内容:

jQuery event.target not working in firefox and IE?

javascript event.target not working in mozilla

我正在尝试获取在选择列表中检查的复选框,以确定要采取的操作。

我的选择列表的ID为event.target

根据chrome调试器和IE,在添加事件之前它会获得正确的元素。

enter image description here

但添加事件参数public void Include<T>(IQueryable<T> source) { MethodInfo include = typeof(EntityFrameworkQueryableExtensions) .GetMethods() .First(x => x.Name == "Include" && x.GetParameters() .Select(y => y.ParameterType.GetGenericTypeDefinition()) .SequenceEqual(new[] { typeof(IQueryable<>), typeof(Expression<>) })); LambdaExpression lambda = GetTestLambdaExpression<T>(); MethodInfo method = include.MakeGenericMethod(typeof(T), lambda.ReturnType); IIncludableQueryable<T, lambda.ReturnType> result = (IIncludableQueryable<T, lambda.ReturnType>)method.Invoke(null, new Object[] { source, parameter }); } 后,将成为选择列表,而不是所选项目。

enter image description here

所以问题归结为在所有三个主要浏览器(Chrome / IE / Firefox)的选择列表中抓取所选元素的正确代码是什么

1 个答案:

答案 0 :(得分:1)

jQuery的.addClass()方法有一个你可以使用的回调。如果将它与jQuery伪造选择器方法结合使用,您可以定位所选内容, 在所有浏览器中兼容。当不同浏览器对HTML元素的行为不同时,最好始终使用通过jQuery规范化的超级特定DOM定位。

<强>文档

.not()方法

.addClass()方法

.removeClass()方法

:selected方法

&#13;
&#13;
//http://stackoverflow.com/a/5754149/5076162
//http://stackoverflow.com/a/22648443/5076162
$('select').on("change", function() {
  $('select option:selected').addClass(function() {
    var isSelected = "isSelected";
    return isSelected;
  });
  var notSelected = $('select option').not($('select option:selected'));
  notSelected.removeClass('isSelected');
});
&#13;
select *.isSelected,
select *.isSelected {
  color: #ACE;
  background-color: #F00;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<select id="optionTest">
  <option value="volvo">Pick your car</option>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>
<div>

</div>
&#13;
&#13;
&#13;

//http://stackoverflow.com/a/5754149/5076162
$('select').on("change", function() {
  $('select option:selected').addClass(function() {
    var isSelected = "isSelected";
        return isSelected;
  });
    var notSelected = $('select option').not($('select option:selected'));
    notSelected.removeClass('isSelected');
});