<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaScript &#8211; 李辉 / Grey Li</title>
	<atom:link href="https://greyli.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>https://greyli.com</link>
	<description>一个编程和写作爱好者的在线记事本</description>
	<lastBuildDate>Thu, 06 Nov 2025 11:36:11 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.9.26</generator>

<image>
	<url>https://greyli.com/wp-content/uploads/2025/03/avatar-500-compressed-144x144.jpg</url>
	<title>JavaScript &#8211; 李辉 / Grey Li</title>
	<link>https://greyli.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>（使用 HTML、JavaScript、Flask 或 Nginx）为丢失的图片显示默认图片</title>
		<link>https://greyli.com/display-default-image-for-missing-image/</link>
		<comments>https://greyli.com/display-default-image-for-missing-image/#respond</comments>
		<pubDate>Sat, 23 Feb 2019 10:58:46 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[Flask]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Nginx]]></category>
		<category><![CDATA[图片]]></category>

		<guid isPermaLink="false">http://greyli.com/?p=2307</guid>
		<description><![CDATA[当在 HTML 页面上显示图片时，如果图片不存在，我们通常需要显示一个默认图片。 假设我们的图片路径在 /im [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>
	当在 HTML 页面上显示图片时，如果图片不存在，我们通常需要显示一个默认图片。
</p>
<p>
	假设我们的图片路径在 /imgs 下，默认图片为 /imgs/default.jpg，下面是一些常见的处理方法示例。
</p>
<h2>
	Solution 1: HTML / JavaScript<br />
</h2>
<p>
	最简单的，你可以使用 &lt;img&gt;&nbsp;元素的&nbsp;<code>onerror</code>&nbsp;属性来设置默认图片：
</p>
<pre>
<code>&lt;img src=&quot;/imgs/cat.jpg&quot; onerror=&quot;this.src=&#39;/imgs/default.jpg&#39;&quot;&gt;</code></pre>
<p>
	类似的，你也可以使用 JavaScript（jQuery）监听 img 元素的 error 事件（当图片地址无效时浏览器会触发这个事件）：
</p>
<pre>
<code>$(&#39;img&#39;).on(&quot;error&quot;, function() {
  $(this).attr(&#39;src&#39;, &#39;/imgs/default.jpg&#39;);  // 替换为默认图片
});</code></pre>
<h2>
	Solution 2: Flask<br />
</h2>
<p>
	除此之外，你也可以在服务器端处理，以 Flask 为例，你可以写一个自定义视图函数来加载图片：
</p>
<pre>
<code>import os
from flask import send_from_directory

# ...

@app.route(&#39;/img/&lt;path:filename&gt;&#39;)
def get_image(filename):
    img_path = os.path.join(images_path, filename)  # 获取图片路径

    if not os.path.exists(img_path):  # 判断图片文件是否存在
        return send_from_directory(os.path.join(images_path, &#39;/imgs/default.jpg&#39;))
    return send_from_directory(img_path)</code></pre>
<p>
	在模板里，使用这个视图函数获取图片 URL：
</p>
<pre>
<code>&lt;img src=&quot;{{ url_for(&#39;get_image&#39;, filename=</code>&#39;imgs/&#39;&nbsp;+ <code>image.filename) }}&quot; &gt;</code></pre>
<h2>
	Solution 3: Nginx<br />
</h2>
<p>
	在生产环境下，出于性能的考虑，我们通常会使用 Web 服务器（比如 Nginx / Apache）&nbsp;来服务（serve）静态文件。以 Nginx 为例，你可以使用&nbsp;<a href="http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files"><code>try_files</code></a>&nbsp;指令来设置默认图片：
</p>
<pre>
<code>location /imgs/ {
    try_files $uri /imgs/default.jpg;
}</code></pre>
<p>
	附注：本文改写自我的<a href="https://stackoverflow.com/a/54684256/5511849">SO&nbsp;回答</a>。</p>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/display-default-image-for-missing-image/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pomodoro Clock（番茄时钟）</title>
		<link>https://greyli.com/pomodoro-clock-clock-of-tomato/</link>
		<comments>https://greyli.com/pomodoro-clock-clock-of-tomato/#respond</comments>
		<pubDate>Sun, 11 Dec 2016 13:06:32 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://greyli.com/?p=1311</guid>
		<description><![CDATA[上次用Flask做了一个计时器，没有实现番茄时钟的功能，这次用JavaScript完成了。 Demo：http [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>上次用Flask做了一个计时器，没有实现番茄时钟的功能，这次用JavaScript完成了。</p>
<p><a href="http://greyli.com/wp-content/uploads/2016/12/demo.png" rel="attachment wp-att-1312"><img class="aligncenter size-full wp-image-1312" src="http://greyli.com/wp-content/uploads/2016/12/demo.png" alt="pomodoro clock" width="837" height="539" srcset="https://greyli.com/wp-content/uploads/2016/12/demo.png 837w, https://greyli.com/wp-content/uploads/2016/12/demo-150x97.png 150w, https://greyli.com/wp-content/uploads/2016/12/demo-300x193.png 300w, https://greyli.com/wp-content/uploads/2016/12/demo-624x402.png 624w" sizes="(max-width: 837px) 100vw, 837px" /></a></p>
<p>Demo：<a href="https://greyli.github.io/pomodoro" target="_blank">https://greyli.github.io/pomodoro</a><a href="http://codepen.io/greyli/full/bBYYJJ/" target="_blank"><br /></a>源码：<a href="https://github.com/greyli/pomodoro" target="_blank">https://github.com/greyli/pomodoro</a></p>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/pomodoro-clock-clock-of-tomato/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript计算器</title>
		<link>https://greyli.com/javascript-calculator/</link>
		<comments>https://greyli.com/javascript-calculator/#respond</comments>
		<pubDate>Tue, 06 Dec 2016 04:04:11 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://greyli.com/?p=1302</guid>
		<description><![CDATA[用JavaScript做了一个计算器，大部分时间都花在完善样式和交互上了，现在还想着再给它添加一个主题。我发现 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>用JavaScript做了一个计算器，大部分时间都花在完善样式和交互上了，现在还想着再给它添加一个主题。我发现，我喜欢把一个东西从丑变美的过程，好看比好用更吸引我。这不是一个好习惯……<a href="http://greyli.com/wp-content/uploads/2016/12/calculator.png" rel="attachment wp-att-1294"><img class="aligncenter size-full wp-image-1294" src="http://greyli.com/wp-content/uploads/2016/12/calculator.png" alt="calculator" width="651" height="552" srcset="https://greyli.com/wp-content/uploads/2016/12/calculator.png 651w, https://greyli.com/wp-content/uploads/2016/12/calculator-150x127.png 150w, https://greyli.com/wp-content/uploads/2016/12/calculator-300x254.png 300w, https://greyli.com/wp-content/uploads/2016/12/calculator-624x529.png 624w" sizes="(max-width: 651px) 100vw, 651px" /></a><a href="http://codepen.io/greyli/full/bBYYJJ/" target="_blank"><br /></a>Demo：<a href="https://greyli.github.io/calculator/" target="_blank">https://greyli.github.io/calculator/</a><a href="http://codepen.io/greyli/full/bBYYJJ/" target="_blank"><br /></a>源码：<a href="https://github.com/greyli/calculator" target="_blank">https://github.com/greyli/calculator</a></p>
<p>&nbsp;</p>
<h2><b>优化交互、美化样式</b></h2>
<p>在这个计算器里，用到了一些处理技巧，可以让它看起来更真实和漂亮。</p>
<p><strong>计算器边缘阴影</strong></p>
<p>这里使用box-shadow的inset方法，也就是把阴影内嵌到元素里，让计算器看起来是有厚度的：</p>
<pre class="">box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), inset -1px -6px 12px 0.1px #89847e;</pre>
<p>参考内容：<a href="https://css-tricks.com/snippets/css/css-box-shadow/" target="_blank">https://css-tricks.com/snippets/css/css-box-shadow/</a></p>
<p><b>按钮按下效果</b></p>
<p>其实是设置按钮的box-shadow，按下时把box-shadow设为none，同时按钮向下移动</p>
<pre lang="css">button {
  box-shadow: 1px 2px #666;
}

button:active {
  box-shadow: none;
  transform: translateY(4px);
}</pre>
<p><b>按钮上的字不可选择</b></p>
<p>双击按钮或是拖动按钮选择会出现蓝色背景色，设置user-select去掉这个特性</p>
<pre lang="css">.un-selectable {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}</pre>
<p><b>去掉按钮被选中后的蓝色边线</b></p>
<pre lang="css" class="">button:focus {outline:0;}  /* 设为none效果相同，或加上 !important */
</pre>
<p class="" lang="css"> </p>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/javascript-calculator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>typing.js——打字机效果</title>
		<link>https://greyli.com/typing-js-typewriter-effect/</link>
		<comments>https://greyli.com/typing-js-typewriter-effect/#comments</comments>
		<pubDate>Sat, 03 Sep 2016 08:54:37 +0000</pubDate>
		<dc:creator><![CDATA[李辉]]></dc:creator>
				<category><![CDATA[计算机与编程]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://withlihui.com/?p=1003</guid>
		<description><![CDATA[前一段时间，无意间在某个人的个人页面看到一个很有趣的打字机效果，可以自动的打出一段文字，删掉，然后打出另一段文 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>前一段时间，无意间在某个人的个人页面看到一个很有趣的打字机效果，可以自动的打出一段文字，删掉，然后打出另一段文字。经过半天的尝试，自己写了一个出来。因为没有系统学过JavaScript（只看过一本JavaScript编程精解……），写出来的代码不够简洁和优雅，嵌套了太多if语句。欢迎到Github上提交修改。</p>
<p>GitHub项目地址： <a href="https://github.com/greyli/typing.js" target="_blank" rel="noopener">https://github.com/greyli/typing.js</a></p>
<h2>DEMO</h2>
<p>打字机效果很适合用在个人页面和主页的头部，下面是几个示例。</p>
<ul>
<li><a href="http://fanxiangce.com" target="_blank" rel="noopener">翻相册</a>（页脚）</li>
<li><a href="https://productmap.co/" target="_blank" rel="noopener">https://productmap.co/</a></li>
<li><a href="https://productmap.co/" target="_blank" rel="noopener">http://www.stephanemartinw.com/</a></li>
</ul>
<h2>用法</h2>
<p>首先把CSS和JS文件放到相应的目录下，然后在HTML文件里分别引入这两个文件：</p>
<p>CSS</p>
<pre class="">&lt;link href="typing.css" rel="stylesheet"&gt;</pre>
<p class="">JS</p>
<pre class="">&lt;script src="typing.js"&gt;&lt;/script&gt;</pre>
<p class="">在需要放置的地方插入下面这行</p>
<pre class="">&lt;span id="words"&gt;&lt;/span&gt;&lt;span id="cursor"&gt;|&lt;/span&gt;</pre>
<p class="">cursor是文字后面闪烁的光标，你可以更换它。</p>
<p class="">最后在末尾设置你要定义的字段和相关的设置。首先你需要定义一个数组，然后写入单个或多个字段。</p>
<pre class="">&lt;script&gt;
  var strings = new Array("一段文字"); // 单个字段
&lt;/script&gt;</pre>
<pre class="">&lt;script&gt;
  var strings = new Array("文段1", "文段2"); // 多个字段
&lt;/script&gt;</pre>
<p class="">可选的设置有打字速度和删除速度，是否循环（默认为循环）等。</p>
<pre class="">&lt;script&gt;
  var strings = new Array("文段1", "文段2") ; // multi words
  var typingSpeed = 100; // 打出每个字的间隔时间
  var deleteSpeed = 40; // 删除每个字的间隔时间
  var isLoop = true; // 是否循环，true/false
  var waitTime = 800; // 打完一个字段后的等待时间
&lt;/script&gt;</pre>
<h2>最后</h2>
<p>如果你在使用这个JS库，那么请让我知道，我会把你加入DEMO里。 </p>
]]></content:encoded>
			<wfw:commentRss>https://greyli.com/typing-js-typewriter-effect/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
