Maison > interface Web > tutoriel HTML > le corps du texte

在web开发中有用的9个技巧提示

巴扎黑
Libérer: 2017-03-19 17:26:26
original
1323 Les gens l'ont consulté

  在本文中,我们给出 9 个有用的 HTML、CSS 和 JavaScript 的技巧和提示,可能在做 Web 开发中经常会需要用到的,其中有几个是关于 HTML5 和 CSS3 的,如果你是一个前端开发者,那么或许对你有些用处。

tips and tricks

1. 使用 html5 的 placeholder 属性

  以前我们经常要写不少JavaScript 的代码来实现现在HTML5 的 placeholder 属性的功能,一个输入框在没获取焦点时显示某个提示信息,当获得输入焦点就自动清除提示信息,目前支持该属性的浏览器有:Opera 11+, Firefox 9+, Safari 5+, IE 10+,不过下面提供的代码对于不支持 placeholder 的浏览器也适用:

// jQuery code
var i = document.createElement("input");
 
// Only bind if placeholder isn't natively supported by the browser
if (!("placeholder" in i)) {
    $("input[placeholder]").each(function () {
        var self = $(this);
        self.val(self.attr("placeholder")).bind({
            focus: function () {
                if (self.val() === self.attr("placeholder")) {
                    self.val("");
                }
            },
            blur: function () {
                var label = self.attr("placeholder");
                if (label && self.val() === "") {
                    self.val(label);
                }
            }
        });
    });
}
 
<!-- html5 -->
<input type="text" name="search" placeholder="Search" value="">
Copier après la connexion

2. 使用 font face

  你可以通过 font face 来使用一些更好的独特的字体,支持多数浏览器:Opera 11+, Firefox 3+, Safari 5, IE6+

@font-face {
    font-family: &#39;MyWebFont&#39;;
    src: url(&#39;webfont.eot&#39;); /* IE9 Compat Modes */
    src: url(&#39;webfont.eot?#iefix&#39;) format(&#39;embedded-opentype&#39;), /* IE6-IE8 */
         url(&#39;webfont.woff&#39;) format(&#39;woff&#39;), /* Modern Browsers */
         url(&#39;webfont.ttf&#39;)  format(&#39;truetype&#39;), /* Safari, Android, iOS */
         url(&#39;webfont.svg#svgFontName&#39;) format(&#39;svg&#39;); /* Legacy iOS */
    }
     
body {
       font-family: &#39;MyWebFont&#39;, Fallback, sans-serif;
}
Copier après la connexion

3. Box Sizing

  好吧,我会说这是我最近最喜欢的CSS属性。它可以解决布局问题。例如,当您添加一个textfield填充,宽度将是文本框的宽度+填充,这很烦人,它通常将打破布局。然而,通过使用这个属性,它解决了这个问题。

 支持的浏览器:Opera 8.5+, Firefox 1+, Safari 3, IE8+, Chrome 4+

textarea {
    -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
    -moz-box-sizing: border-box;    /* Firefox, other Gecko */
    box-sizing: border-box;         /* Opera/IE 8+ */
}
Copier après la connexion

4. 禁用 Textarea 的大小改变

  有些时候你不需要用户可以改变多行文本输入口 textarea 的大小,可是一些基于 Webkit 的浏览器(例如 safari 和 chrome)就可以让用户随意更改 textarea 大小,好在你可以禁用这个特性:

textarea {
    resize: none
}
Copier après la connexion

5.jQuery.trim()

  用来去除字符串前后的空格:

$.trim("       a lot of white spaces, front and back!      ");
Copier après la connexion

6. jQuery.inArray()

  用来判断某个元素是否在数组之中:

var arr = [ "xml", "html", "css", "js" ];
$.inArray("js", arr);
Copier après la connexion

7. 编写一个简单的 jQuery 插件(模板)

//You need an anonymous function to wrap around your function to avoid conflict
(function($){
  
    //Attach this new method to jQuery
    $.fn.extend({
          
        //This is where you write your plugin&#39;s name
        pluginname: function() {
  
            //options
            var defaults = {
                option1: "default_value"
            }
             
            var options = $.extend(defaults, options);
  
            //a public method
            this.methodName: function () {
                //call this method via $.pluginname().methodName();
            }
  
            //Iterate over the current set of matched elements
            return this.each(function() {
              
                var o = options;
              
                //code to be inserted here
              
            });
        }
    });
      
//pass jQuery to the function,
//So that we will able to use any valid Javascript variable name
//to replace "$" SIGN. But, we&#39;ll stick to $ (I like dollar sign: ) )      
})(jQuery);
Copier après la connexion

8. 扩展 jQuery 选择器的功能

jQuery.expr[&#39;:&#39;].regex = function(elem, index, match) {
    var matchParams = match[3].split(&#39;,&#39;),
        validLabels = /^(data|css):/,
        attr = {
            method: matchParams[0].match(validLabels) ?
                        matchParams[0].split(&#39;:&#39;)[0] : &#39;attr&#39;,
            property: matchParams.shift().replace(validLabels,&#39;&#39;)
        },
        regexFlags = &#39;ig&#39;,
        regex = new RegExp(matchParams.join(&#39;&#39;).replace(/^s+|s+$/g,&#39;&#39;), regexFlags);
    return regex.test(jQuery(elem)[attr.method](attr.property));
}
 
/******** Usage ********/
 
// Select all elements with an ID starting a vowel:
$(&#39;:regex(id,^[aeiou])&#39;);
  
// Select all ps with classes that contain numbers:
$(&#39;p:regex(class,[0-9])&#39;);
  
// Select all SCRIPT tags with a SRC containing jQuery:
$(&#39;script:regex(src,jQuery)&#39;);
Copier après la connexion

9. 优化并降低 PNG 图像文件的大小

  你可以通过降低颜色数来降低png文件的大小,详情请看 PNG file optimization

结论

  前端开发是一个非常有趣的领域,我们永远不能能学到一切。每次我工作的一个新项目,我总是觉得自己发现一些新的或奇怪的东西。我觉得这几个技巧,将是非常方便和有用的;)

英文原文:9-useful-tips-and-tricks-for-web-developers

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!