简体   繁体   English

普通JavaScript等于jquery $(this)

[英]plain JavaScript equal to jquery $(this)

I currently have code for jQuery for links. 我目前有jQuery链接代码。 When a link in a selected for is clicked it opens dialog. 单击所选对象中的链接时,将打开对话框。

$('.dialog_link_add').click(function(){
    var row_id = $(this).parent().parent().attr('id');
    return false;
}

Because dialog always makes window to scroll to the top (in Internet Explorer), i've found solution to this by using plain JavaScript and implementing method the old fasion way. 因为对话框总是使窗口滚动到顶部(在Internet Explorer中),所以我已经找到解决方案,方法是使用纯JavaScript并以旧的流行方式实现方法。

onclick="function_call(this);return false;"

... but it's not working. ...但是它不起作用。 How can I now send "this" parameter to be equal to jQuery $(this)? 我现在如何发送“ this”参数等于jQuery $(this)?

The answer to the question you actually asked is: The raw equivalent of $(this) is this , but with the form of your given onclick , it would be the argument you pass into your function. 这个问题的答案,你居然问的问题是:的原始相当于$(this)this ,而是你给予的形式onclick ,这将是你传递到你的函数的参数。 You can continue to use jQuery in that function if you like, just accept the element as an argument (because that's how you're passing it) and then use $() on it: 如果愿意,您可以继续在该函数中使用jQuery,只接受该元素作为参数(因为这就是传递它的方式),然后在其上使用$()

function function_call(elm) {
    var row_id = $(elm).parent().parent().attr('id');
    // ...
}

But to go completely raw DOM:: 不过要完全原始DOM ::

function function_call(elm) {
    var row_id = elm.parentNode.parentNode.id;
}

More in the various DOM specs and HTML5 API: 有关各种DOM规范和HTML5 API的更多信息:

BUT : You can prevent the scroll in your jQuery version in the same way: 但是 :您可以通过以下方式防止jQuery版本中的滚动:

 $('.dialog_link_add').click(function(){ var row_id = $(this).parent().parent().attr('id'); return false; // <<============= }); 

In a jQuery handler, return false does two things: 在jQuery处理程序中, return false做两件事:

  1. Prevent the default action (so in the case of a link in the form # , it doesn't scroll the window). 阻止默认操作(因此,如果链接为#形式,则不会滚动窗口)。

  2. Prevent the event from bubbling up the DOM. 防止事件使DOM冒泡。

In a DOM0-style handler (your onclick ), return false prevents the default action, but doesn't stop bubbling. 在DOM0样式的处理程序( onclick )中, return false阻止默认操作,但不会停止冒泡。 More about return false in various types of handlers . 有关各种类型的处理程序中的return false更多信息 (The edited question shows a return false in the jQuery handler that wasn't originally there.) (已编辑的问题显示了原本不存在的jQuery处理程序中的return false 。)

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

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