简体   繁体   English

为什么我的JavaScript无法正常工作?

[英]Why won't my javascript work?

Here's my javascript code (backslashes are for escaping): 这是我的JavaScript代码(反斜杠用于转义):

<script type="text/javascript">
        $(function(){
            //datepicker
            $(\'#start\').datepicker({
                inline: true
            });
        });
</script>

And the relevant HTML: 以及相关的HTML:

<input type="text" name="start" id="start" class="hasDatepicker" />

It's essentially identical to the example code that comes with jquery and jquery ui. 它与jquery和jquery ui随附的示例代码基本相同。 I'm thinking it could be that everything is appended to a variable called $content and echoed out at the end instead of simply being HTML. 我在想可能是所有内容都附加到了一个名为$ content的变量上,并在最后回显,而不是简单地成为HTML。

I appreciate any answers to this conundrum. 感谢您对此难题的任何解答。

I should perhaps have been more visual about how my code is set up instead of explaining it at the end. 我也许应该更直观地了解我的代码是如何设置的,而不是在最后进行解释。 This is generally how it looks: 通常是这样的:

$content .= '<script type="text/javascript">
        $(function(){
            //datepicker
            $(\'#start\').datepicker({
                inline: true
            });
        });
</script>';

My HTML is similarly added to $content: 我的HTML也类似地添加到$ content中:

$content .= '<input type="text" name="start" id="start" class="hasDatepicker" />';

Did you try without the backslashes? 您是否尝试了没有反斜杠?

<script type="text/javascript">
        $(function(){
            //datepicker
            $('#start').datepicker({
                inline: true
            });
        });
</script>

Nothing to escape really 真的没有逃脱

AND: 和:

Do you use firebug or any other web debugging tool? 您是否使用Firebug或任何其他Web调试工具?

Response to Updated Question 对更新问题的回答

Your $content variable will output the following: 您的$content变量将输出以下内容:

<script type="text/javascript">
  $(function(){
    //datepicker
    $('#start').datepicker({
      inline: true
    });
  });
</script>

This works as-is (assuming no problems with your page): http://jsbin.com/oxekuw/2/edit 这可以按原样工作(假设您的页面没有问题): http : //jsbin.com/oxekuw/2/edit

I would encourage you to check your console output (typically accessible by hitting F12) for any message regarding $ or datepicker method not being found. 我鼓励您检查控制台输出(通常可通过按F12进行访问),以获取有关未找到$datepicker方法的任何消息。 This would indicate their was a problem linking up either jQuery or jQuery UI. 这表明他们在链接jQuery或jQuery UI时出现问题。

Use the following as a template if you need to. 如果需要,请使用以下内容作为模板。 It is also available online if you would like to do testing there: http://jsbin.com/oxekuw/3/edit 如果您想在那里进行测试,也可以在线获得: http : //jsbin.com/oxekuw/3/edit

<!DOCTYPE html>
<html>
  <head>
    <title>Datepicker</title>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" />
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.19/jquery-ui.js"></script>
    <script type="text/javascript">
      $(function(){
        //datepicker
        $('#start').datepicker({
          inline: true
        });
      });
    </script>
  </head>
  <body>
    <input id="start" type="text" />
  </body>
</html>

Response to Original Question 对原始问题的回应

You escape when single quotes are used within a string that is encapsulated by single quotes. 当在由单引号引起来的字符串中使用单引号时,您将逃脱。 You don't escape here though: 您虽然没有逃脱:

$(\'#start\'); // No-no

Check your console output and you'll likely see something to the effect of: SyntaxError: Unexpected token ILLEGAL 检查控制台输出,您可能会看到以下效果: SyntaxError: Unexpected token ILLEGAL

There is a time and place for you to escape characters in your selector though. 虽然有时间和地点可以让您在选择器中转义字符。 The following would be an example of when you would escape: 以下是您何时退出的示例:

$("input[name=\"foo\"]"); // Little more sensible

Note, double-quotes within double-quotes. 请注意,双引号内的双引号。 We have to distinguish our inside ones from the outside ones so that we don't terminate the string prematurely. 我们必须将内部字符串与外部字符串区分开,以免过早地终止字符串。

The better solution is to stagger your double/single quotes. 更好的解决方案是错开双引号/单引号。 If you use double quotes on the outside, use single quotes on the inside: 如果在外部使用双引号,请在内部使用单引号:

$("input[name='foo']"); // Much easier on the eyes

As others noted, your code should work. 正如其他人指出的那样,您的代码应该可以工作。

One common problem which could cause problems is that you have multiple HTML elements with that same ID ( id="start" ). 一个可能引起问题的常见问题是您有多个具有相同ID( id="start" )的HTML元素。

是否可以在创建输入元素之前运行datepicker()代码?

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

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