简体   繁体   English

js中的JSON.stringify

[英]JSON.stringify in js

How do I get reference of the <div> id and title from a external JS by using the code below: 如何使用以下代码从外部JS获取<div> id和标题的引用:

function recordedEvent() {    
    var v_Id = $(this).attr('Id');
    var v_Title = $(this).attr('Title');
    var o = { Title : v_Title, ObjectId : v_Id };
    alert(JSON.stringify(o));
}

The function is called in the HTML with a onclick called box1() . 在HTML中使用名为box1()onclick调用该函数。

Code in CplTemplateSetup.js is where I want to run the function from into the HTML: 我要在CplTemplateSetup.js代码CplTemplateSetup.js功能从HTML中运行:

content_left_30_four_click_images_right_70_head_content_template.html

Any help would be appreciated. 任何帮助,将不胜感激。


PS : JSON data ( zip archive ) PSJSON数据( zip存档

Well the most obvious problem is that you don't have a closing parenthesis after your callback.. otherwise the code looks good 好吧,最明显的问题是回调后没有右括号。.否则代码看起来不错


Edit 编辑

window.lastClickedBoxData = {}; // just reassign that within your function

or 要么

window.runThisWhenClicked = function () {
    var v_Id = $(this).attr('Id');
    var v_Title = $(this).attr('Title');
    var o = { Title : v_Title, ObjectId : v_Id };
};

then just 然后就

$(".box").click(window.runThisWhenClicked);

I don't think that the problem that you're facing would be apparent to anyone who doesn't view your code. 我认为您所面临的问题对于任何不查看您的代码的人来说都是显而易见的。

You defined a function within a script tag, in the html, and called that function in the onclick attribute of the box being clicked on. 您在html的script标签内定义了一个函数,并在被单击框的onclick属性中调用了该函数。 To that function, this refers to the box that was clicked on. 对于该功能, this是指单击的框。 From there, you call a function within an external js document. 从那里,您可以在外部js文档中调用一个函数。 In that function, this refers to the window object. 在该函数中, this引用窗口对象。 In other words, for the code that you posted, $(this) is a jquery instance of the window object which doesn't have the attributes that you're looking for, such as id and title , so they're blank. 换句话说,对于您发布的代码, $(this)是window对象的jquery实例,它没有您要查找的属性,例如idtitle ,因此为空。

To see what I'm talking about open the console and add the following line of code to each of your functions: 要查看我在说什么,请打开控制台并将以下代码行添加到每个函数中:

console.log(this);

If you're having trouble doing that, the following code should work as well, but it's not as good for debugging: 如果您在执行此操作时遇到麻烦,则以下代码也应该可以正常工作,但对于调试而言效果不佳:

alert(this);

You need all of your code to be in the html script tag or in the external document. 您需要所有代码都位于html脚本标记或外部文档中。 Also, you shouldn't define the function to be called during a click event using onclick within the html. 此外,您不应在html中使用onclick定义在click事件期间要调用的函数。 It's better to do it using javascript or jquery so that your javascript is completely separate from your html. 最好使用javascript或jquery做到这一点,以使您的javascript与html完全分开。

EDIT : You shouldn't update your question with an answer. 编辑 :您不应该使用答案来更新您的问题。 You should edit the original question with this information so that anyone having a similar problem can find the solution. 您应该使用此信息编辑原始问题,以便任何有类似问题的人都可以找到解决方案。 I'd have commented on the answer directly, but I don't have the reputation to do this. 我会直接对答案发表评论,但我没有做到这一点的声誉。

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

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