我有一个页面,其中包含如下事件:
$(document).on("click","#div",function(e){
//do the operation
});
此事件在页面加载和div单击时执行。 我需要区分事件中的这两个调用,因为我必须在div点击而不是加载时写一个额外的条件。
如何知道页面加载或div点击?
修改
载入时:
$(默认).find(' a'。)触发器("点击");使其在加载时触发
答案 0 :(得分:2)
$(document).on("click", "#div", function(e) {
if (e.originalEvent !== undefined) {//use the originalEvent
alert('human');
} else {
alert('load click')
}
});
$('#div').click();

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='div'>click</div>
&#13;
答案 1 :(得分:2)
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
using std::map;
template <typename Map>
struct my_node_writer {
// my_node_writer() {}
my_node_writer(Map& g_) : g (g_) {};
template <class Vertex>
void operator()(std::ostream& out, Vertex v) {
out << " [label=\"" << v << "\"]" << std::endl;
};
Map g;
};
template <typename Map>
my_node_writer<Map> node_writer(Map& map) { return my_node_writer<Map>(map); }
template <typename Map>
struct my_edge_writer {
my_edge_writer(Map& g_) : g (g_) {};
template <class Edge>
void operator()(std::ostream& out, Edge e) {
out << " [color=purple]" << std::endl;
out << " [label=\"" << e <<":" << g[e].miles << "\"]" << std::endl;
};
Map g;
};
template <typename Map>
my_edge_writer<Map> edge_writer(Map& map) { return my_edge_writer<Map>(map); }
struct my_graph_writer {
void operator()(std::ostream& out) const {
out << "graph [bgcolor=lightgrey]" << std::endl;
out << "node [shape=circle color=blue]" << std::endl;
out << "edge [color=red]" << std::endl;
}
} myGraphWrite;
struct edge_prop
{
int miles;
};
int main()
{
using namespace boost;
typedef adjacency_list<vecS, vecS, directedS, no_property, edge_prop> Graph;
Graph g;
add_edge(0, 1, edge_prop{123}, g);
write_graphviz(std::cout,g,node_writer(g),edge_writer(g),myGraphWrite);
}
&#13;
//You can check for originalEvent element present in event object.
$(document).ready(function() {
$(document).on("click","#div",function(e){
if(e.originalEvent) {
alert('clicked manually');
} else {
alert('page load');
}
});
$('#div').click();
});
&#13;
答案 2 :(得分:1)
你可以在每个中设置一个布尔变量,如:
<OperationTypes xmlns="http://www.example.com/aaa/2011" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<OperationType Uri="http://localhost:8012/Enterprise/OperationTypes/a1d02dac-c454-497b-808d-a619009c7c7e" Name="test" Number="19" Active="true" Description="" SetupCalculation="PerWorkOrder" Side="" />
<OperationType Uri="http://localhost:8012/Enterprise/OperationTypes/07102c89-93e1-499b-980e-a61900cf2217" Name="OT_Prakash" Number="20" Active="false" Description="" SetupCalculation="PerWorkOrder" Side="" />
</OperationTypes>
然后使用if或switch语句检查哪个条件为真。
<强> [编辑] 强> 除非您希望警报突然出现,否则其他答案会提示设置变量,因为这样就可以了。
答案 3 :(得分:0)
如果您需要对页面加载执行任何操作,则必须指定如下
$(document).load(function(){
alert("event on page load");
});
如果div点击事件,则如下
$(#div).onclick(function(){
alert ("on div click");
})