简体   繁体   English

如何生成jQuery代码段

[英]How to generate a jQuery snippet

I am trying to generate a simple jQuery code snippet in JavaScript and append it to one of the divs already available in my html file. 我试图在JavaScript中生成一个简单的jQuery代码片段,并将其附加到我的html文件中已经可用的div之一。 Here is the string I am trying to append as an html content of the existing div element in my html file: 这是我要作为html文件中现有div元素的html内容追加的字符串:

<form><div id=\"radio\"><input type=\"radio\" id=\"radio1\" name=\"radio\" /><label 
for=\"radio1\">Choice 1</label><input type=\"radio\" id=\"radio2\" name=\"radio\" /><label 
for=\"radio2\">Choice 2</label><input type=\"radio\" id=\"radio3\" name=\"radio\" /><label 
for=\"radio3\">Choice 3</label></div></form>

If you try the following code on JSFiddle : 如果您在JSFiddle上尝试以下代码:

$('div#new').append('<form><div id=\"radio\"><input type=\"radio\" id=\"radio1\" name=\"radio\" /><label 
for=\"radio1\">Choice 1</label><input type=\"radio\" id=\"radio2\" name=\"radio\" /><label 
for=\"radio2\">Choice 2</label><input type=\"radio\" id=\"radio3\" name=\"radio\" /><label 
for=\"radio3\">Choice 3</label></div></form>')

The Radio elements are displayed but with no jQuery visual style. 显示了Radio元素,但没有jQuery视觉样式。 When trying the same string in a static html file (see below) the visual style appears correctly. 在静态html文件中尝试相同的字符串时(请参见下文),视觉样式会正确显示。 Would appreciate your help. 感谢您的帮助。

PS: I am trying to avoid jquery\\html\\JS template so would be great it I could get this code working with no needs of templates. PS:我正在尝试避免使用jquery \\ html \\ JS模板,所以这太好了,我可以使此代码无需模板就可以工作。

Thanks, Jamil 谢谢,贾米尔

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" />
    <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>
</head>

<body>
    <form>
        <div id="radio">
            <input type="radio" id="radio1" name="radio" />
            <label for="radio1">Choice 1</label>

            <input type="radio" id="radio2" name="radio" />
            <label for="radio2">Choice 2</label>

            <input type="radio" id="radio3" name="radio" />
            <label for="radio3">Choice 3</label>
        </div>
    </form>
</body>
</html>

seems to work just fine... 似乎工作得很好...

http://jsfiddle.net/E2zTn/10/ http://jsfiddle.net/E2zTn/10/

All i did was add 我所做的就是添加

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" />
    <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>
</head>

<body>

....

</body>
</html>

I'm not sure I completely understand from your code what you're trying to do as it doesn't entirely match your description, but have you tried something like: 我不确定我是否从您的代码中完全理解了您要尝试执行的操作,因为它与您的描述不完全匹配,但是您尝试了以下操作:

$("#radio").append('<input type="radio" id="radio1" name="radio" /><label 
for="radio1">Choice 1</label><input type="radio" id="radio2" name="radio" /><label 
for="radio2">Choice 2</label><input type="radio" id="radio3" name="radio" /><label 
for="radio3">Choice 3</label>');

The difference is that rather than adding stuff outside #radio you are adding it inside instead. 区别在于,不是在#radio之外添加东西,而是在内部添加它。 Does that help? 有帮助吗?

Thanks! 谢谢! Neil 尼尔

元素上的.trigger('create')将迫使元素重新渲染,以拾取所有必要的样式。

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

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