简体   繁体   English

jQuery onclick事件/具有通用onclick事件的参数的范围

[英]scope for jquery onclick event/parameters with generic onclick event

How would I capture additional details in a div/span tag with a generic onclick event? 如何通过通用的onclick事件在div / span标记中捕获其他详细信息?

Ie, 也就是说,

<span id=item1>Value #001<span id=clickableitem>xxx</span></span>
<span id=item2>Value #002<span id=clickableitem>xxx</span></span>
<span id=item3>Value #003<span id=clickableitem>xxx</span></span>

And then: 接着:

$(document).on('click','#clickableitem',function(e){
e.preventDefault();

// how do I get the "value #001, #002, #003" value just 'outside'
// of the span tag?

I was trying to figure it out with .parent().html(), but that didn't quite seem to work... 我试图用.parent()。html()弄清楚,但这似乎不太起作用...

Thanks! 谢谢!

The id property should always be unique, instead give your span's a common className and hook up the event handler callback with it, also, in your HTML you need to put single or double quotes around the values of properties such as id and class: id属性应该始终是唯一的,而为您的跨度指定一个通用的className并使用它连接事件处理程序回调,此外,在HTML中,您需要在属性值(例如id和class)周围加上单引号或双引号:

HTML: HTML:

<span id='item1'>Value #001<span id='clickableitem1' class='spanClickable'>xxx</span></span>
<span id='item2'>Value #002<span id='clickableitem2' class='spanClickable'>xxx</span></span>
<span id='item3'>Value #003<span id='clickableitem3' class='spanClickable'>xxx</span></span>

JQuery: JQuery的:

$(document).on('click','.spanClickable',function(e){
   e.preventDefault();
   //Using .text() on the parent produces the child span text as well
   //Replace the child text and the word Value since all you want is 
   //ex. #001
   const childSpanText = $(this).text();
   alert($(this).parent().text().replace(childSpanText, "").replace('Value ', ''));
});

Working Fiddle ( https://jsfiddle.net/a5Lkon4j/2/ ) 工作小提琴( https://jsfiddle.net/a5Lkon4j/2/

HTML: HTML:

<span id=item1>Value #001<span class="clickableitem">xxx</span></span>
<span id=item2>Value #002<span class="clickableitem">xxx</span></span>
<span id=item3>Value #003<span class="clickableitem">xxx</span></span>

JS: JS:

$('.clickableitem').click(function() {
    let text = $(this).parent().text().substring(0, $(this).parent().text().indexOf($(this).text()));

    console.log(text);
});

Replacing the text is unsafe could be resulting in an empty text if the value is the same as xxxx child span.... 如果值与xxxx子跨度相同,则替换文本不安全可能会导致文本为空。

This aproach is more effective 这种方法更有效

HTML HTML

<span id='item1'>Value #001<span id='clickableitem1' class='spanClickable'>xxx</span></span>
<span id='item2'>Value #002<span id='clickableitem2' class='spanClickable'>xxx</span></span>
<span id='item3'>Value #003<span id='clickableitem3' class='spanClickable'>xxx</span></span>

JS JS

$(document).on('click','.spanClickable',function(e){
   alert( $(this).parent().contents().get(0).nodeValue  );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM