条件 HTML 需要建议
这两种情况哪个更好。只是确保我使用最佳实践。
<% if(isEdit){ %>
<input type="text" id="tabtitle" name="title" value=<%=Content%>/>
<%}else{%>
<input type="text" id="title" name="title"/>
<%}%>
或者
$(document).ready(function() {
<% if(isEdit){ %>
$("#title").val("<%=Content%>");
<%}%>
});
What will be better of these 2 cases. Just making sure I am using best practice.
<% if(isEdit){ %>
<input type="text" id="tabtitle" name="title" value=<%=Content%>/>
<%}else{%>
<input type="text" id="title" name="title"/>
<%}%>
OR
$(document).ready(function() {
<% if(isEdit){ %>
$("#title").val("<%=Content%>");
<%}%>
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第二个使用 JavaScript / JQuery 修改元素。由于添加客户端代码没有任何优势,因此我将采用第一种仅在服务器端的方法。尽管没有看到更大的前景,但也很难特别认可这种方法。
The second uses JavaScript / JQuery to modify the element. Since adding client-side code has no advantage I would take the first approach which is server-side only. Although without seeing the bigger picture it's hard to endorse this approach particularly either.
我会采用第一种方法,因为条件无论如何都存在,并且 javascript 不会向功能添加任何内容。这只是一种被迫的做法。
此外,如果您使用服务器端,您的代码在访问浏览器时将是正确的,并且调用 javacsript 函数不会有延迟。有时,当你通过 javascript 更改 dom 时,会出现闪烁或擦除...没有它你会更好。
I would go by the first approach because the conditional is there anyway, and the javascript doesn't add anything to the functionality. It's just a forced approach.
Additionally if you use server side, your code is going to be correct the time it hits the browser, and there will be no lag for the javacsript function to be called. Sometimes there's a flash, or a wipe when you change dom through javascript... You will be better without it.
我更喜欢第一种方法,因为出于安全原因,某些用户通常会禁用 Javascript
I'll prefer the first approach as Javascript is usually disabled by some users for security reasons
我会采纳 Pointy 的建议并在 JSP 中使用表达式语言 (EL)。您可以将:
将您的 titleid 属性设置为“title”或“tabtitle”,并将 Content 设置为“”或您的内容。我还没有测试过,但我相信这会完成同样的事情。
I would go with Pointy's suggestion and use the expression language (EL) in your JSP. You could just put:
Set your titleid property to "title" or "tabtitle" and Content to "" or your content. I haven't tested it but I believe that would accomplish the same thing.