我可以干掉这些 jQuery 调用吗?
有没有办法让这个 jQuery 干燥起来?
<script type="text/javascript">
$('form input#search').watermark('search...');
</script>
<script type="text/javascript">
$('form input#post_title').watermark('title');
</script>
<script type="text/javascript">
$('form input#post_tag_list').watermark('tag (separate tags with a comma)');
</script>
<script type="text/javascript">
$('form input#post_name').watermark('name (optional)');
</script>
<script type="text/javascript">
$('form input#post_email').watermark('email (optional)');
</script>
<script type="text/javascript">
$('form textarea#post_content').watermark('message');
</script>
<script type="text/javascript">
$('form input#comment_commenter').watermark('name (optional)');
</script>
<script type="text/javascript">
$('form input#comment_email').watermark('email (optional)');
</script>
<script type="text/javascript">
$('form textarea#comment_body').watermark('reply');
</script>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("abbr.timeago").timeago();
});
</script>
看起来重复得非常厉害。
编辑: 我向所有表单添加了占位符元素。我的应用程序是 HTML5,所以没问题。我使用了这段代码:
<script type="text/javascript">
jQuery(function(){
jQuery("abbr.timeago").timeago();
jQuery('form input, form textarea').each(
function(){
jThis = jQuery(this);
jThis.watermark(jThis.attr('placeholder'));
}
}
);
</script>
Chrome 使用或不使用 JS 渲染占位符,而 FF 3.6.8 和 Opera 10.61 显示空的输入/文本区域框。我应该使用 $
而不是 jQuery(function(){...
吗?或者这很重要吗?
注意:我正在使用 jQuery 1.4.2
Is there a way to DRY this jQuery up?
<script type="text/javascript">
$('form input#search').watermark('search...');
</script>
<script type="text/javascript">
$('form input#post_title').watermark('title');
</script>
<script type="text/javascript">
$('form input#post_tag_list').watermark('tag (separate tags with a comma)');
</script>
<script type="text/javascript">
$('form input#post_name').watermark('name (optional)');
</script>
<script type="text/javascript">
$('form input#post_email').watermark('email (optional)');
</script>
<script type="text/javascript">
$('form textarea#post_content').watermark('message');
</script>
<script type="text/javascript">
$('form input#comment_commenter').watermark('name (optional)');
</script>
<script type="text/javascript">
$('form input#comment_email').watermark('email (optional)');
</script>
<script type="text/javascript">
$('form textarea#comment_body').watermark('reply');
</script>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("abbr.timeago").timeago();
});
</script>
It seems awfully repetitive.
EDIT:
I added placeholder elements to all my forms. My app is HTML5 so it's okay. I used this code:
<script type="text/javascript">
jQuery(function(){
jQuery("abbr.timeago").timeago();
jQuery('form input, form textarea').each(
function(){
jThis = jQuery(this);
jThis.watermark(jThis.attr('placeholder'));
}
}
);
</script>
Chrome renders the placeholders with or without JS, while FF 3.6.8 and Opera 10.61 show empty input/textarea boxes. Should I be using $
instead of jQuery(function(){...
? Or does it matter?
note: I'm using jQuery 1.4.2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您将水印功能的参数存储在标题属性中,那么您可能会得到如下内容:
If you stored the parameter for the watermark function in the title attributes then you could have something like this;
您可以将其存储在一个对象中,因为您拥有所有 ID,然后您可以执行以下操作:
you could store it in an object since you have all ID's you can then do this: