我有以下jquery代码,点击div元素后会发生一个动作。
我想将相同的点击操作Canvas.showContentDoc(d);
添加到#document
元素中的 href 。
如果用户点击<i class='fa fa-angle-double-down'></i>
,我希望采取相同的操作,我该如何实现?
for (node in obj.nodes) {
var d = obj.nodes[node];
var color_document = "blue"
jQuery('<div/>', {
id: 'document' + d.id,
class: 'document',
click: function (d) {
Canvas.showContentDoc(d);
}
}).appendTo(title)
$('#document' + d.id).append("<span class='fa-stack fa-1x'> <i class='fa fa-circle fa-stack-2x' style='color:rgba(110,130,34, 1)'></i><i class='fa fa-file fa-stack-1x' style='color:" + color_document + "'></i></span> : " + d.label + " (" + d.id_doc + ")<a href='#'> <i class='fa fa-angle-double-down'></i> </a> ");
Canvas.selectedNodesInfos[nodes_iteration + id_start]['label'] = d.label;
nodes_iteration += 1;
}
答案 0 :(得分:2)
它就像jQuery中的this一样简单:
$("selector1, selector2, selector3").on("click", function(){
Canvas.showContentDoc(d);
});
在jsFiddle上查看或试用此演示:
$(document).ready(function(){
$("#button_1, #span_1, #anchor_1").on("click", function(){
sameMethod();
});
});
function sameMethod(){
alert("same method called.");
console.log("same method called.")
}
.clickable{
display: block;
padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<button id = "button_1" class = "clickable">
Click Me
</button>
<span id = "span_1" class = "clickable">
Click Me
</span>
<a href="#" id = "anchor_1" class = "clickable">Click Me</a>
</div>
答案 1 :(得分:0)
您可以使用.querySelector()
添加事件监听器,如下所示:
document.querySelector('li.fa.fa-angle-double-down').addEventListener('click', function() {
Canvas.showContentDoc(d);
});
这是一个非常简单的例子:
document.querySelector('li.fa.fa-angle-double-down').addEventListener('click', function() {
alert('Clicked!');
});
<li class="fa fa-angle-double-down">List item</li>
答案 2 :(得分:0)
您可以将<i class='fa fa-angle-double-down'></i>
打包在<a>
示例:<a href="#" onclick="Canvas.showContentDoc(d);"> <i class='fa fa-angle-double-down'></i></a>
虽然我相信一路走JS更好看@dsharew回答