[英]How to pass DOM element and JSON object to JavaScript function if the code is generated?
I have the following jQuery/Javascript code snippet. 我有以下jQuery / Javascript代码段。 This snippet is intended to insert a new div
element as a child of a DOM Element that already exists on the page. 此摘要旨在将新的div
元素作为页面上已存在的DOM元素的子元素插入。 The div
element contains a text label "myLabel". div
元素包含文本标签“ myLabel”。 When someone clicks on "myLabel", I would like it to call myFunction
with two parameters: one JSON object and one DOMElement. 当有人单击“ myLabel”时,我希望它使用两个参数调用myFunction
:一个JSON对象和一个DOMElement。
$(document).ready(function() {
// myDOMElement is a DOM object acquired by doing document.getElementById()
// myFunction is a Javascript function defined elsewhere
myJSONObject = {"Hello": "World"};
var newDiv = document.createElement('div');
newDiv.setAttribute("id", "myNewDivID");
newDiv.innerHTML = "<span id=\"mySpanID\" onclick=\"myFunction('" + myJSONObject +"', '" + myDOMElement + "');\">myLabel</span>";
});
This code doesn't work when I click on "myLabel". 当我单击“ myLabel”时,此代码不起作用。
The problem lies in the way I am passing in myJSONObject
and myDOMElement
to myFunction
. 问题在于我将myJSONObject
和myDOMElement
传递给myFunction
。
In myFunction
they get printed out as the strings " [object Object]
" and " [object HTMLDivElement]
" respectively. 在myFunction
它们分别作为字符串“ [object Object]
”和“ [object HTMLDivElement]
”输出。 Obviously this is because of the single-quote I have inserted just inside the parenthesis in the call to the function. 显然,这是因为我在函数调用中的括号内插入了单引号。 But if I remove those single quotes, then it doesn't work because the definition of onclick
now contains syntax errors. 但是,如果我删除了这些单引号,那么它将不起作用,因为onclick
的定义现在包含语法错误。
How can I escape strings or parse/de-parse around this issue so that I can manipulate myDOMElement
and myJSONObject
in myFunction
? 如何围绕该问题转义字符串或进行解析/反解析,以便可以在myFunction
操纵myDOMElement
和myJSONObject
?
You're obviously using jQuery, so use it 您显然正在使用jQuery,因此请使用它
$(document).ready(function() {
var myJSONObject = {"Hello": "World"};
var newDiv = $('<div />', {id : 'myNewDivID'}),
var span = $('<span />', {id : 'mySpanID', text : 'myLabel'});
span.on('click', function() {
// use myJSONObject and "this" here
});
$('#target_element').append( newDiv.append(span) );
});
You can attach the click handler dynamically, that is: 您可以动态附加点击处理程序,即:
function myFunction(jsonObject, element) {
console.log(jsonObject);
console.log(element);
}
$(document).ready(function() {
myJSONObject = {"Hello": "World"};
var newDiv = $('<div id="myNewDivID"/>');
var span = $('<span id="mySpanID">');
span.text('myLabel');
span.on('click', myFunction.bind(undefined, myJSONObject, 'this would be the element'));
newDiv.append(span);
});
Calling myFunction.bind()
returns a new function with the arguments to myFunction
preset. 调用myFunction.bind()
将返回一个带有myFunction
预设参数的新函数。 The first argument to bind
is what this
will be set to, but because jQuery will (I think) use call
to call the event handler, it doesn't really matter. 第一个参数bind
是什么this
将被设置为,而是因为jQuery将(我认为)使用call
调用事件处理程序,它其实并不重要。
If you want access to the event as well, you can add a accept another argument to myFunction
, ie: 如果还希望访问事件,则可以在myFunction
添加一个accept另一个参数,即:
function myFunction(jsonObject, element, event) {
// Do things.
}
Without jQuery: 没有jQuery:
function myFunction(jsonObject, element) {
console.log(jsonObject);
console.log(element);
}
$(document).ready(function() {
myJSONObject = {"Hello": "World"};
var div = document.createElement('div');
div.setAttribute('id', 'myNewDivId');
var span = document.createElement('span');
span.setAttribute('id', 'mySpanId');
span.innerText = 'words'; // or textContent?
span.addEventListener('click', myFunction.bind(window, myJSONObject, 'this would be the element'));
div.appendChild(span);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.