• $('a').click()无法触发页面跳转

    描述

    在用$("a").click()方法, 触发a元素点击事件,进行页面跳转效果. 结果发现页面未发生跳转.

    示例

    <html>
    <meta charset='utf-8'>
    <body>
    <button id="btn">按钮</button>
    <a href="http://www.baidu.com" target="_blank" id="link">链接</a>
    </body>
    <script src="/jquery1.91.js" type="text/javascript"></script>
    <script>
    $(function(){
        $("#btn").click(function(e){
            //触发点击事件
            $("a").click();
        });
    </script>
    </html>
    

    解决方法

    • 第一种: 用原生的a对象触发事件
    $("a")[0].click();
    
    • 第二种: 通过事件冒泡触发事件
    <a href="http://www.baidu.com" target="_blank" id="link"><span id="link">链接</span></a>
    
    $(function(){
        $("#btn").click(function(e){
            //触发点击事件
            $("#link").click();
        });
    </script>
    
  • git免密码(https方式)

    使用git push提交时,每次都要输入密码,次数多了,感觉挺麻烦. 如果git以ssh协议通讯,免密码可以用ssh公钥设置免登录。如果git时以https方式访问呢,该怎么办?下面方式可以解决这个问题.
    
    • 新建文件并保存密码
    touch ~/.git-credentials
    vim ~/.git-credentials
    

    添加内容

    https://{username}:{password}@github.com
    
    • 添加git配置

    执行下面命令添加配置

    git config --global credential.helper store
    
    • 查看~/.gitconfig文件变化

    ~/.gitconfig文件多出下面配置项

    [credential]
        helper = store
    

    再尝试git push不再需要输入密码.

  • 运行jetty-maven-plugin出现Unable to load the mojo 'run' in the plugin

    在运行jetty-maven-plugin出现错误

    [ERROR] Failed to execute goal org.eclipse.jetty:jetty-maven-plugin:9.2.6.v20141205:run (default-cli) on project PKPlatform_web: Execution default-cli of goal org.eclipse.jetty:jetty-maven-plugin:9.2.6.v20141205:run failed: Unable to load the mojo 'run' in the plugin 'org.eclipse.jetty:jetty-maven-plugin:9.2.6.v20141205' due to an API incompatibility: org.codehaus.plexus.component.repository.exception.ComponentLookupException: org/eclipse/jetty/maven/plugin/JettyRunMojo : Unsupported major.minor version 51.0
    [ERROR] -----------------------------------------------------
    [ERROR] realm =    plugin>org.eclipse.jetty:jetty-maven-plugin:9.2.6.v20141205
    [ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
    [ERROR] urls[0] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/jetty-maven-plugin/9.2.6.v20141205/jetty-maven-plugin-9.2.6.v20141205.jar
    [ERROR] urls[1] = file:/D:/DEV/mavenRepons/org/sonatype/sisu/sisu-inject-bean/2.1.1/sisu-inject-bean-2.1.1.jar
    [ERROR] urls[2] = file:/D:/DEV/mavenRepons/org/sonatype/sisu/sisu-guice/2.9.4/sisu-guice-2.9.4-no_aop.jar
    [ERROR] urls[3] = file:/D:/DEV/mavenRepons/org/codehaus/plexus/plexus-utils/2.0.6/plexus-utils-2.0.6.jar
    [ERROR] urls[4] = file:/D:/DEV/mavenRepons/org/sonatype/aether/aether-util/1.11/aether-util-1.11.jar
    [ERROR] urls[5] = file:/D:/DEV/mavenRepons/org/codehaus/plexus/plexus-interpolation/1.14/plexus-interpolation-1.14.jar
    [ERROR] urls[6] = file:/D:/DEV/mavenRepons/org/codehaus/plexus/plexus-component-annotations/1.5.5/plexus-component-annotations-1.5.5.jar
    [ERROR] urls[7] = file:/D:/DEV/mavenRepons/org/sonatype/plexus/plexus-sec-dispatcher/1.3/plexus-sec-dispatcher-1.3.jar
    [ERROR] urls[8] = file:/D:/DEV/mavenRepons/org/sonatype/plexus/plexus-cipher/1.4/plexus-cipher-1.4.jar
    [ERROR] urls[9] = file:/D:/DEV/mavenRepons/org/apache/maven/plugin-tools/maven-plugin-tools-api/3.1/maven-plugin-tools-api-3.1.jar
    [ERROR] urls[10] = file:/D:/DEV/mavenRepons/backport-util-concurrent/backport-util-concurrent/3.1/backport-util-concurrent-3.1.jar
    [ERROR] urls[11] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/jetty-util/9.2.6.v20141205/jetty-util-9.2.6.v20141205.jar
    [ERROR] urls[12] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/jetty-webapp/9.2.6.v20141205/jetty-webapp-9.2.6.v20141205.jar
    [ERROR] urls[13] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/jetty-xml/9.2.6.v20141205/jetty-xml-9.2.6.v20141205.jar
    [ERROR] urls[14] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/jetty-servlet/9.2.6.v20141205/jetty-servlet-9.2.6.v20141205.jar
    [ERROR] urls[15] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/jetty-quickstart/9.2.6.v20141205/jetty-quickstart-9.2.6.v20141205.jar
    [ERROR] urls[16] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/jetty-jaas/9.2.6.v20141205/jetty-jaas-9.2.6.v20141205.jar
    [ERROR] urls[17] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/jetty-security/9.2.6.v20141205/jetty-security-9.2.6.v20141205.jar
    [ERROR] urls[18] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/jetty-plus/9.2.6.v20141205/jetty-plus-9.2.6.v20141205.jar
    [ERROR] urls[19] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/jetty-jndi/9.2.6.v20141205/jetty-jndi-9.2.6.v20141205.jar
    [ERROR] urls[20] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/jetty-server/9.2.6.v20141205/jetty-server-9.2.6.v20141205.jar
    [ERROR] urls[21] = file:/D:/DEV/mavenRepons/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar
    [ERROR] urls[22] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/jetty-http/9.2.6.v20141205/jetty-http-9.2.6.v20141205.jar
    [ERROR] urls[23] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/jetty-io/9.2.6.v20141205/jetty-io-9.2.6.v20141205.jar
    [ERROR] urls[24] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/jetty-jmx/9.2.6.v20141205/jetty-jmx-9.2.6.v20141205.jar
    [ERROR] urls[25] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/jetty-annotations/9.2.6.v20141205/jetty-annotations-9.2.6.v20141205.jar
    [ERROR] urls[26] = file:/D:/DEV/mavenRepons/javax/annotation/javax.annotation-api/1.2/javax.annotation-api-1.2.jar
    [ERROR] urls[27] = file:/D:/DEV/mavenRepons/org/ow2/asm/asm/5.0.1/asm-5.0.1.jar
    [ERROR] urls[28] = file:/D:/DEV/mavenRepons/org/ow2/asm/asm-commons/5.0.1/asm-commons-5.0.1.jar
    [ERROR] urls[29] = file:/D:/DEV/mavenRepons/org/ow2/asm/asm-tree/5.0.1/asm-tree-5.0.1.jar
    [ERROR] urls[30] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/websocket/javax-websocket-server-impl/9.2.6.v20141205/javax-websocket-server-impl-9.2.6.v20141205.jar
    [ERROR] urls[31] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/websocket/javax-websocket-client-impl/9.2.6.v20141205/javax-websocket-client-impl-9.2.6.v20141205.jar
    [ERROR] urls[32] = file:/D:/DEV/mavenRepons/javax/websocket/javax.websocket-api/1.0/javax.websocket-api-1.0.jar
    [ERROR] urls[33] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/websocket/websocket-server/9.2.6.v20141205/websocket-server-9.2.6.v20141205.jar
    [ERROR] urls[34] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/websocket/websocket-common/9.2.6.v20141205/websocket-common-9.2.6.v20141205.jar
    [ERROR] urls[35] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/websocket/websocket-api/9.2.6.v20141205/websocket-api-9.2.6.v20141205.jar
    [ERROR] urls[36] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/websocket/websocket-client/9.2.6.v20141205/websocket-client-9.2.6.v20141205.jar
    [ERROR] urls[37] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/websocket/websocket-servlet/9.2.6.v20141205/websocket-servlet-9.2.6.v20141205.jar
    [ERROR] urls[38] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/apache-jsp/9.2.6.v20141205/apache-jsp-9.2.6.v20141205.jar
    [ERROR] urls[39] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/toolchain/jetty-schemas/3.1.M0/jetty-schemas-3.1.M0.jar
    [ERROR] urls[40] = file:/D:/DEV/mavenRepons/org/mortbay/jasper/apache-jsp/8.0.9.M3/apache-jsp-8.0.9.M3.jar
    [ERROR] urls[41] = file:/D:/DEV/mavenRepons/org/mortbay/jasper/apache-el/8.0.9.M3/apache-el-8.0.9.M3.jar
    [ERROR] urls[42] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/orbit/org.eclipse.jdt.core/3.8.2.v20130121/org.eclipse.jdt.core-3.8.2.v20130121.jar
    [ERROR] urls[43] = file:/D:/DEV/mavenRepons/org/eclipse/jetty/apache-jstl/9.2.6.v20141205/apache-jstl-9.2.6.v20141205.jar
    [ERROR] urls[44] = file:/D:/DEV/mavenRepons/org/apache/taglibs/taglibs-standard-spec/1.2.1/taglibs-standard-spec-1.2.1.jar
    [ERROR] urls[45] = file:/D:/DEV/mavenRepons/org/apache/taglibs/taglibs-standard-impl/1.2.1/taglibs-standard-impl-1.2.1.jar
    [ERROR] urls[46] = file:/D:/DEV/mavenRepons/javax/transaction/javax.transaction-api/1.2/javax.transaction-api-1.2.jar
    [ERROR] Number of foreign imports: 1
    [ERROR] import: Entry[import  from realm ClassRealm[maven.api, parent: null]]
    [ERROR]
    [ERROR] -----------------------------------------------------
    [ERROR] -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginContainerException
    

    从上面发现

    repository.exception.ComponentLookupException: org/eclipse/jetty/maven/plugin/JettyRunMojo : Unsupported major.minor version 51.0
    

    很明显是版本问题. 检查机器, jdk版本是1.6,改为1.后,重新运行,一切正常.

  • 加载图片失败,怎样替换为默认图片

    img元素加载图片失败,则变成一个小图标,让页面变得难看。此时如何替换为默认图片?

    onerror属性

    img元素自带onerror属性,加载失败时,触发error事件

    <img src="image.gif" onerror='this.src="default.gif" />
    

    jquery.error函数

    jquery提供对应的事件处理函数

    $('img').error(function(){
        $(this).attr('src',"default.gif");
    })
    

    jquery.one函数

    使用上面两种方法,假如默认图片也加载失败,则变成死循环. 此时可使用one()绑定事件

    $("img").one("error", function(e){
         $(this).attr("src", "default.gif");
    });
    

    另外error事件,不支持冒泡,jquery.delegate函数捕捉不到error事件。

  • jetty-maven-plugin静态文件保存不了

    起因

    为maven项目配置jetty-maven-plugin插件,以jetty:run方式运行,会出现静态文件保存不了的问题。原因是jetty内存映射文件来缓存静态文件. 解决方法是将jetty内存映射参数取消掉.

    解决

    • 找webdefault.xml文件
      在下面路径找到webdefault.xml文件
    C:\Users\Administrator\.m2\repository\org\eclipse\jetty\jetty-webapp\9.2.6.v20141205
    

    如果安装了jetty服务器,在它的etc目录下也可以取到webdefault.xml文件

    • useFileMappedBuffer参数值改为false
      webdefault.xml配置
    <init-param>
      <param-name>useFileMappedBuffer</param-name>
      <param-value>false</param-value>
    </init-param>
    
    • 新增defaultsDescriptor配置项
    <plugin>  
        <groupId>org.eclipse.jetty</groupId>  
        <artifactId>jetty-maven-plugin</artifactId>  
        <version>9.2.6.v20141205</version>  
        <configuration>
            <scanIntervalSeconds>0</scanIntervalSeconds>
            <webAppSourceDirectory>${basedir}/WebRoot</webAppSourceDirectory>
            <webApp>  
                <contextPath>/</contextPath>
                <war>${project.build.directory}/${project.build.finalName}.war</war>
                <!-- jetty9.2.6的defaultsDescriptor在<webApp>里,早期版本在<configuration>或者<webAppConfig>里 -->
                <defaultsDescriptor>${basedir}/WebRoot/resources/webdefault.xml</defaultsDescriptor>
            </webApp>
        </configuration>
    </plugin>
    
    • webdefault.xml加载是否成功
      控制台输出如下,表示加载了webdefualt.xml
    [INFO] Web defaults = .... /WebRoot/resources/webdefault.xml
    

    感受

    上面方法虽然可以解决问题,但是很不好用. 如果jetty-maven-plugin能设计个开发模式设置,在易用性上会有更大提升.

    参考资料

  • PTY allocation request failed on channel 0 是什么意思?

    描述

    在测试ssh与git的连接状况时出现

    ➜  ~  ssh git@github.com
    PTY allocation request failed on channel 0
    Hi miaoyin! You've successfully authenticated, but GitHub does not provide shell access.
    Connection to github.com closed.
    

    提示含义

    PTY allocation request failed on channel 0
    

    上面表示分配PTY失败。PTY指伪终端. 它可以让用户,像在本地shell终端操作一样的操作远程机器. 可以加上参数-T,表示仅建立数据通道.

    设置参数-T

    ➜  ~ ssh -T git@github.com
    Hi miaoyin! You've successfully authenticated, but GitHub does not provide shell access.
    

    某些情况下-T很有用,如将ssh作为数据通道.

  • WARN:oejsh.ContextHandler:Aliased resource 错误

    在为maven项目配置了jetty-maven-plugin插件,以jetty:run方式运行之后,控制台出现

    2014-12-13 10:30:58.531:WARN:oejsh.ContextHandler:Aliased resource: file:/D:/Workspaces/MyEclipse%2010/PKPlatform_bg/WebRoot/pkBg/js/pgManage.js~=
    file:/D:/Workspaces/MyEclipse%2010/PKPlatform_bg/WebRoot/pkBg/js/pgManage.js
    

    发现引用路径中,多了个斜杠,去掉就可以了

    <script type="text/javascript" src="//js/pgManage.js"></script>
    
  • 让网站在浏览器标签上显示小图标

    • 制作宽高均为32像素的小图表,完成后存储为ico格式

    • 添加到页面中

    <link rel="shortcut ico" type="image/x-icon" href="/imsages/favicon.ico">
    
  • chrome跳到daohang.114so.cn

    昨天选好喜欢的名字, 注册了个cn域名,今天审核通过. 有点小高兴. 先配置好域名解析. 然后设置好github的CNAME文件. 域名解析是即刻生效,github的CNAME要等会儿了. 拿着域名在chrome上访问,直接调到http://daohang.114so.cn/ ,在IE上却一切正常. 试了各种方法还是不行,只有在后面随便加个参数先.

  • 在自己的网页中调用搜索引擎

    示例

    下面是常见示例,先动手试试

    <meta charset="UTF-8" />
    <body>
        <!-- 调用google搜索 -->
        <form method=get action="http://www.google.com/search"  target="_blank">
            <!-- input的中name必须为q,否则提交失败,返回google首页-->
            <input type=text name=q size=40 maxlength=255 >
            <!--input type=hidden name=sitesearch value="yongqing.is-programmer.com"-->
            <input type=submit name=btnG value="谷歌搜索">
        </form>
    
        <!-- 调用baidu搜索 -->
        <form action="http://www.baidu.com/baidu" target="_blank">
            <!-- input的中name必须为word,否则提交失败,返回baidu首页-->
            <input type=text name=word size=40>
            <!--input type=hidden name=si value="yongqing.is-programmer.com"-->
            <input type="submit" value="百度搜索">
        </form>
        <!-- 调用yahoo搜索-->
        <form action="http://www.yahoo.com.cn/search">
            <input type=text name="p">
            <!--input type=hidden name=vs value="yongqing.is-programmer.com"-->
            <input type="submit" value="Yahoo 搜索">
        </form>
    </body>
    

    调用google的参数

    发现还可以传递其它参数,它们有什么作用?

    <!–Google站内搜索–>
    <form method=get action=”http://www.google.com/search”>
        <input type=text name=q>
        <input type=submit name=btnG value=”Google 搜索”>
        <input type=hidden name=sitesearch value=”yongqing.is-programmer.com”>
        <input type=hidden name=hl value=zh-CN>
        <input type=hidden name=ie value=utf-8>
        <input type=hidden name=oe value=utf-8>
    </form>
    
    • name="q": 搜索的字符串
    • name="sitesearch": 指定要搜索的网站.
    • name="hl": 以指定语言显示搜索结果. 比如"zh-CN", "zh-TW", "en"
    • name="ie": 指明搜索网页的编码,默认是UTF-8.
    • name="oe": 以指定编码显示搜索结果,默认是UTF-8.
    • name="safe"
    • name="newwindow"

    仔细看google首页的源码,发现也用这样的调用方式.
    如果想要搜索指定网站的网页,google比较好用,比较适合方在个人博客中,作为搜索工具
    百度、yahoo就不介绍了,有兴趣可以研究下.