Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.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 - Fatal编程技术网

Javascript 如何找出被点击的内容?

Javascript 如何找出被点击的内容?,javascript,jquery,Javascript,Jquery,在jquery中,我如何确定单击了什么对象 有没有办法提醒(…)标签的类型?就像是一个或等?查看 具体地说,事件请查看 具体来说,事件可以添加onclick事件来警告属性 <p id='pWoo' onclick='alert(this.id);'>Woo</p> 求爱 不特定于jquery。可以添加onclick事件来警告属性 <p id='pWoo' onclick='alert(this.id);'>Woo</p> 求爱 不特定于jq

在jquery中,我如何确定单击了什么对象

有没有办法提醒(…)标签的类型?就像是一个
等?

查看

具体地说,事件

请查看


具体来说,事件可以添加onclick事件来警告属性

<p id='pWoo' onclick='alert(this.id);'>Woo</p>

求爱


不特定于jquery。

可以添加onclick事件来警告属性

<p id='pWoo' onclick='alert(this.id);'>Woo</p>

求爱


不特定于jquery。

单击的对象作为
this
传递到单击处理程序中。您可以使用
nodeName
查找元素的类型。像这样:

function handle_click() {
   var clicked_element = this;
   alert(clicked_element.nodeName);
}
$("#mylink").click(handle_click);

单击的对象作为
this
传递到单击处理程序中。您可以使用
nodeName
查找元素的类型。像这样:

function handle_click() {
   var clicked_element = this;
   alert(clicked_element.nodeName);
}
$("#mylink").click(handle_click);
只要您想知道处理事件的元素的类型(您将事件附加到的内容),则此选项是正确的。如果您想确切地知道单击了哪个元素,包括该元素的子元素,则需要该属性。以麦格纳为例:

// show the type of the element that event handler was bound to
function handle_click() {
    var clicked_element = this;
    alert(clicked_element.nodeName);
}

// show the type of the exact element that was clicked, which may be a child
// of the bound element
function handle_child_click(e) {
    var clicked_element = e.target;
    alert(clicked_element.nodeName);
}

// show the type of the element that matches "#myLink"
$("#mylink").click(handle_click);

// show the type of the element that matches "#myLink" or any of its children
$("#mylink").click(handle_child_click);
只要您想知道处理事件的元素的类型(您将事件附加到的内容),则此选项是正确的。如果您想确切地知道单击了哪个元素,包括该元素的子元素,则需要该属性。以麦格纳为例:

// show the type of the element that event handler was bound to
function handle_click() {
    var clicked_element = this;
    alert(clicked_element.nodeName);
}

// show the type of the exact element that was clicked, which may be a child
// of the bound element
function handle_child_click(e) {
    var clicked_element = e.target;
    alert(clicked_element.nodeName);
}

// show the type of the element that matches "#myLink"
$("#mylink").click(handle_click);

// show the type of the element that matches "#myLink" or any of its children
$("#mylink").click(handle_child_click);

可能不是您想要的,但我发现本教程很有趣:

可能不是您想要的,但我发现本教程很有趣:

看起来您的示例html标记被过滤掉了。它们只是随机的例子,还是其中有重要的东西?看起来你的html标签样本被过滤掉了。它们只是随机的例子,还是其中有重要的东西?实际上,您可能不应该在HTML中嵌入JavaScript。。。您最好使用不引人注目的JavaScript方法()。实际上,您可能不应该在HTML中嵌入JavaScript。。。最好使用不引人注目的JavaScript方法()。