jQuery代码仍然被执行,即使被注释掉 [英] jQuery code still being executed even after being commented out

查看:603
本文介绍了jQuery代码仍然被执行,即使被注释掉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的.js文件与这里面它

I have a basic .js file with this inside it

//$('#show').html('<%= escape_javascript(render(:partial => 'show')) %>');

.js文件被调用时,上面的代码被执行,部分被渲染,即使它注释掉。当代码被删除时,部分从不被渲染。 DOM保持不变,但我可以看到部分由服务器在命令行呈现。

When the .js file is called, the code above gets executed and the partial is rendered even though it's commented out. When the code is deleted, the partial never gets rendered. The DOM remains unchanged, but I can see the partial being rendered by the server at the command line. What gives?

推荐答案

您正在注释掉在客户端执行的JavaScript。

You're commenting out the JavaScript--which executes on the client side.

部分渲染发生在服务器端,在客户端看到渲染的JavaScript之前。

The partial rendering happens on the server side, before the client ever sees the rendered JavaScript.

换句话说,注释掉JavaScript零效果在服务器端处理。如果您不想显示服务器端字符串,请注释:

In other words, commenting out JavaScript has zero effect on the server-side processing. If you don't want the server-side string to be displayed, comment it out:

<%#= escape_javascript(etc) %>






让我们假设部分渲染如下: p>


Let's assume the partial renders like this:

<h1>Foo bar baz</h1>
<div>Plugh!</div>

对JavaScript进行转义会将换行符变成 \\\
(可以转义单引号和双引号等等)。

Escaping this for JavaScript will turn the newline into a \n (and would escape single- and double-quotes, etc.) leaving you with this on the client side:

$('#show').html('<h1>Foo bar baz</h1>\n<div>Plugh!</div>');

无论JS是否注释掉,部分将渲染,除非你注释掉 escape_javascript Ruby代码的结果。

Whether or not the JS is commented out or not, the partial is going to render unless you comment out the results of the escape_javascript Ruby code.

在客户端,如果JS被注释掉, 't update show 的HTML - 你说的是什么吗?

On the client side, if the JS is commented out, it shouldn't update show's HTML--are you saying it is?

这篇关于jQuery代码仍然被执行,即使被注释掉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆