[英]jQuery, function .html correct syntax
Somehow I ran into a syntax problem. 我不知何故遇到了语法问题。 I have this code here
我这里有这段代码
$('#logoutput').html("<div id=\"loginbox\" class=\"boxgrid\">Username: <form action=\"\" method=\"post\"><input class=\"textfieldcss\" type=\"text\" name=\"loginname\" value="+$userip+"\">Password: <form action=\"\" method=\"post\"><input class=\"textfieldcss\" type=\"hidden\" name=\"loginpass\" value="+$userpw+"\"></div>");
if the content only is: 如果仅内容是:
$('#logoutput').html("<div id=\"loginbox\" class=\"boxgrid\">Username: <form action=\"\" method=\"post\"><input class=\"textfieldcss\" type=\"text\" name=\"loginname\">"
it doesn't show me an error. 它没有显示我一个错误。
Could this be because I implemented the variables wrong into the jQuery function? 难道是因为我在jQuery函数中实现了错误的变量?
Not a true answer to your question, but it'll help you make this far less problematic: don't use strings like this. 这不是您问题的真正答案,但是它将帮助您减少问题的发生:不要使用这样的字符串。 At the very least use a template.
至少要使用模板。 There are lots of libraries that do templating for you, but at least do something like:
有很多库可以为您做模板,但是至少可以做一些类似的事情:
<script type="text/html" id="mooTemplate">
<div id="loginbox" class="boxgrid">
Username:
<form action="" method="post">
<input class="textfieldcss" type="text" name="loginname" value="{{ userip }}">
Password:
<form action="" method="post">
<input class="textfieldcss" type="hidden" name="loginpass" value="{{ userpw }}">
</div>
</script>
And then swap it in: 然后将其交换:
var templateHTML = $("#mooTemplate").text()
.replace(/{{ userip }}/, userip)
.replace(/{{ userpw }}/, userpw);
$(target).html(templateHTML);
Although again, don't roll this yourself, just use one of the many premade templating solutions available on the web. 再次提醒您,不要自己动手,只需使用网上提供的许多预制模板解决方案之一即可。 I personally like nunjucks, but there are tons of others.
我个人喜欢小女孩,但还有很多其他的人。
这似乎可行:
$('#logoutput').html('<div id=\"loginbox\" class=\"boxgrid\">Username: <form action=\"\" method=\"post\"><input class=\"textfieldcss\" type=\"text\" name=\"loginname\" value="'+$userip+'"\">Password: <form action=\"\" method=\"post\"><input class=\"textfieldcss\" type=\"hidden\" name=\"loginpass\" value='+$userpw+'"\"></div>');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.