javascript - 如何在jquery中以编程方式选择可选择的项目?

标签 javascript jquery

请帮帮我!
如何在jquery中以编程方式选择可选择的项目?
我想手动调用mousedown事件

<div id="Explorer">
<!--items-->
</div>
<input type="button" title="Select" click="Select()"/>
<script>
$("#Explorer").selectable();
function Select() { 
//i know following code is incorrect
$("#Explorer").children().eq(4).select();
}
</script>

最佳答案

代码中的几个错误:

1- select() 的使用是错误的。

2- 不要使用 select 名称重新声明函数

$(function() {
  $("#Explorer").selectable();
});

function my_select() {
  $("#Explorer").find('.item').eq(4).addClass('ui-selected');
}
#Explorer .ui-selecting {
  background: #FECA40;
}

#Explorer .ui-selected {
  background: #F39814;
  color: white;
}

#Explorer {
  list-style-type: none;
  margin: 0;
  padding: 0;
  width: 60%;
}

#Explorer p {
  margin: 3px;
  padding: 0.4em;
  font-size: 1.4em;
  height: 18px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet" />

<div id="Explorer">
  <p class="item">Item 1</p>
  <p class="item">Item 2</p>
  <p class="item">Item 3</p>
  <p class="item">Item 4</p>
  <p class="item">Item 5</p>
  <p class="item">Item 6</p>
  <p class="item">Item 7</p>
</div>

<input type="button" title="Select" onclick="my_select()" value="select" />

关于javascript - 如何在jquery中以编程方式选择可选择的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49092374/

相关文章:

javascript - 如何通过单个请求获取多个节点?

javascript - 使用 JavaScript 或 jQuery 创建区分大小写或 'camelCase' 的 XML 节点

javascript - 如何打印出对象的所有属性

jquery - 如何在 Rails 3 中提交到远程

javascript - 在 AngularJS 中不使用指令获取元素属性(高度/宽度)

javascript - 使用 return 与 this 的优势

javascript - Fancyselect 中断更改功能

javascript - jquery/javascript 安全

javascript - 嵌入 Google 地球,在卫星 View 中创建网格布局

javascript - 我可以使用 jQuery 画一条线吗?