[英]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.