简体   繁体   English

如何从HTML元素执行javascript函数并发送该元素名称,而无需专门使用其名称?

[英]How do I execute a javascript function from an HTML element and send that elements name without use its name specifically?

If there is an answer to this question out there I can not find it. 如果有这个问题的答案,我找不到。

I have the following function in JavaScript and need to be able to reuse it over and over. 我在JavaScript具有以下功能,并且需要能够反复使用它。

 function ParentService_load(_Object) { $(_Object.parent.parent).jqxDropDownButton({ ... }); $(_Object).jqxGrid({ ... }); $(_Object).bind('rowselect', function(event) { ... var row = $(_Object).jqxGrid('getrowdata', args.rowindex); ... $(_Object.parent.parent).jqxDropDownButton('setContent', dropDownContent); if (row["servicename"].toString().toLowerCase() === "new") { $(_Object.parent.parent).jqxDropDownButton('close'); ... } }); } 
 <div id="jqxParentServiceDropdownButton"> <div id='jqxParentServiceDropdownWidget' style="font-size: 13px; font-family: Verdana; float: left;"> <div id="jqxParentServiceDropdownGrid" onload="ParentService_load(this);"></div> </div> </div> 

I haven't found a definitive way to call a javascript or jquery function from an element and have that function use that elements' name, etc... So how would I accomplish this? 我还没有找到从元素调用javascript或jquery函数并使该函数使用该元素名称的明确方法,等等。

If by "name" you mean id , then within your function, you can access that as _Object.id . 如果用“名称”表示id ,则在函数中,您可以将其作为_Object.id进行访问。 Because you're passing this in your onload=... (but see below), that's a reference to the element on which the event occurred. 因为您要在onload=...传递this (但请参阅下文),所以它是对发生事件的元素的引用。 Within the function, you're receiving that argument as _Object , so you can use the properties on _Object to access information about the element, including its id . 在函数中,您将以_Object接收该参数,因此可以使用_Object上的属性来访问有关元素的信息,包括其id


But note that div elements don't have a load event. 但请注意, div元素没有load事件。 Also, div elements don't have a parent property; 另外, div元素没有parent属性; you may be thinking of parentNode . 您可能正在考虑parentNode

I'm not completely sure what you are asking, so here are a bunch of examples about returning values embedded in the HTML from a function; 我不确定您要问的是什么,因此这里有很多关于从函数返回嵌入在HTML中的值的示例。

$(document).ready(function(){
  $(".lw").click(function() {
    alert("Class:" + $(this).attr("class") + 
          "\nID:" + $(this).attr("id") + 
          "\nData:" + $(this).data("test") + 
          "\nName:" + $(this).attr("name") + 
          "\nTagname:" + $(this).prop("tagName"));
    alert($(this).parent().prop("tagName"));
  });
});

http://liveweave.com/CZSYqW http://liveweave.com/CZSYqW

Clicking on either of the <p> elements returns a different value in an alert box. 单击任一<p>元素将在警报框中返回不同的值。

暂无
暂无

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

相关问题 当我不带参数动态将其名称作为字符串发送时,如何执行javascript函数 - how to execute javascript function when i send its name as string dynamically with out parameters 当我将其名称作为字符串时如何执行 JavaScript function - How to execute a JavaScript function when I have its name as a string 如何从一个文件执行JavaScript,尤其是页面? - How do I execute JavaScript, page specifically, from one file? 如何从字符串的方法名称中调用JavaScript函数? - How do call a javascript function from its method name in string? 当其名称作为字符串传递给函数时,如何执行JavaScript函数? - How to execute a JavaScript function when its name is passed as a string in function? 当我将其名称作为字符串时执行 JavaScript 函数 - 不工作 - Execute a JavaScript function when I have its name as a string - Not working 如何在Edge中执行名称为字符串的JavaScript函数 - How to execute a JavaScript function having its name as a string in Edge 当我将其名称作为字符串时,如何执行私有JavaScript函数 - How to execute a private JavaScript function when I have its name as a string 如何执行 JavaScript Function 当我将其名称作为字符串时(使用具有重载参数的参数) - How to execute a JavaScript Function When I have its name as a string (with parameters that have an overload argument) 如何通过单击HTML按钮执行JavaScript函数? - How do I execute a JavaScript function from clicking an HTML button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM