我可以干掉这些 jQuery 调用吗?

发布于 2024-09-15 23:50:24 字数 1762 浏览 7 评论 0原文

有没有办法让这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掀纱窥君容 2024-09-22 23:50:24

如果您将水印功能的参数存储在标题属性中,那么您可能会得到如下内容:

<form>
    <input type="text" id="search" title="search..." />
    <input type="text" id="post_title" title="title" />
    <input type="text" id="post_tag_list" title="tag (separate tags with a comma)" />
    <input type="text" id="post_name" title="name (optional)" />
    <input type="text" id="post_email" title="email (optional)" />
    <input type="text" id="post_content" title="message" />
    <input type="text" id="comment_commenter" title="name (optional)" />
    <input type="text" id="comment_email" title="email (optional)" />
    <input type="text" id="comment_body" title="reply" />
</form>

<script type="text/javascript">
    jQuery(function(){
        jQuery("abbr.timeago").timeago();
        jQuery('form input, form textarea').each(
            function(){
                jThis = jQuery(this);
                jThis.watermark(jThis.attr('title'));
            }
        }
    );
</script>

If you stored the parameter for the watermark function in the title attributes then you could have something like this;

<form>
    <input type="text" id="search" title="search..." />
    <input type="text" id="post_title" title="title" />
    <input type="text" id="post_tag_list" title="tag (separate tags with a comma)" />
    <input type="text" id="post_name" title="name (optional)" />
    <input type="text" id="post_email" title="email (optional)" />
    <input type="text" id="post_content" title="message" />
    <input type="text" id="comment_commenter" title="name (optional)" />
    <input type="text" id="comment_email" title="email (optional)" />
    <input type="text" id="comment_body" title="reply" />
</form>

<script type="text/javascript">
    jQuery(function(){
        jQuery("abbr.timeago").timeago();
        jQuery('form input, form textarea').each(
            function(){
                jThis = jQuery(this);
                jThis.watermark(jThis.attr('title'));
            }
        }
    );
</script>
瑶笙 2024-09-22 23:50:24

您可以将其存储在一个对象中,因为您拥有所有 ID,然后您可以执行以下操作:

watermarkText = {};
watermarkText.search = 'search...';
watermarkText.post_title = 'title';
watermarkText.post_tag_list = 'tag (separate tags with a comma)';
watermarkText.post_name = 'name (optional)';
watermarkText.post_email = 'email (optional)';
watermarkText.post_content = 'message';
watermarkText.comment_commenter = 'name (optional)';
watermarkText.comment_email = 'email (optional)';
watermarkText.comment_body = 'reply';

$('input').watermark(function() {return watermarkText[$(this).attr('id')] });
$('textarea').watermark(function() {return watermarkText[$(this).attr('id')]});

you could store it in an object since you have all ID's you can then do this:

watermarkText = {};
watermarkText.search = 'search...';
watermarkText.post_title = 'title';
watermarkText.post_tag_list = 'tag (separate tags with a comma)';
watermarkText.post_name = 'name (optional)';
watermarkText.post_email = 'email (optional)';
watermarkText.post_content = 'message';
watermarkText.comment_commenter = 'name (optional)';
watermarkText.comment_email = 'email (optional)';
watermarkText.comment_body = 'reply';

$('input').watermark(function() {return watermarkText[$(this).attr('id')] });
$('textarea').watermark(function() {return watermarkText[$(this).attr('id')]});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文