码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript笔记杂谈篇(啥都有)

时间:2016-02-14 18:30:20      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:

二维码缩放比例以43PX的倍数缩放最为标准.

NuGet相关管理http://www.cnblogs.com/dudu/archive/2011/07/15/nuget.html

学习笔记: http://kb.cnblogs.com/page/143190/

 

动态创建标记,给标记添加样式,class,等等   vardtProductName=$("<dt></dt>",{style:"text-overflow: ellipsis;overflow: hidden;white-space: nowrap;"});

CSS样式选择器

定义和用法

:last-child 选择器匹配属于其父元素的最后一个子元素的每个元素。

提示:p:last-child 等同于 p:nth-last-child(1)。

 

在JavaScript中,使用childNodes属性可以返回一个数组,这个数组包含给定元素节点的全体子节点,如代码所示:

01

<body>

02

<ul id="action">

 

03

    <li title="第一段文字">第一个</li>

04

    <li title="第二段文字">第二个</li>

 

05

</ul>

06

<script type="text/javascript">

 

07

var attr_p = document.getElementById("action");

08

alert(attr_p.childNodes[1].childNodes[0].nodeValue);

 

09

</script>

10

</body>

如果要取得id为action的ul的第一个li内的文本节点(如取得:第一个),可以使 用…childNodes[1].childNodes[0].nodeValue这种方法找到,使 用…childNodes[1].firstChild.nodeValue同样可以找到第一个li的文本节点,
结论childNodes[0]等价于firstChild,无论何时何地,重要需要访问childNodes[]数组的第一个元素,我们就可以把它写成firstChild,DOM还提供一个与之对应的lastChild属性。
需要注意的是,ff的空格节点问题,可以使用nodeType属性对节点类型判断,直到发现元素节点为止

 

 

通过 JavaScript 调用

在 CSS 中添加 position: relative; 之后,通过 JavaScript 代码启动滚动监听插件:

复制

$(‘body‘).scrollspy({ target: ‘#navbar-example‘ })

方法

.scrollspy(‘refresh‘)

当使用滚动监听插件的同时在 DOM 中添加或删除元素后,你需要像下面这样调用此刷新( refresh) 方法:

复制

$(‘[data-spy="scroll"]‘).each(function () { var $spy = $(this).scrollspy(‘refresh‘) })

参数

可以通过 data 属性或 JavaScript 传递参数。对于 data 属性,其名称是将参数名附着到 data- 后面组成,例如 data-offset=""

名称

类型

默认值

描述

offset

number

10

计算滚动位置时相对于顶部的偏移量(像素数)。

事件

事件类型

描述

activate.bs.scrollspy

每当一个新条目被激活后都将由滚动监听插件触发此事件。

 

$(‘#myScrollspy‘).on(‘activate.bs.scrollspy‘, function () { // do something… })

NuGet  安装指定版本的程序包教程

1,NuGet程序命令

http://docs.nuget.org/consume/package-manager-console-powershell-reference

2教程

 

使用Nuget 安装指定版本package或者更新package到指定版本

 

最近在琢磨MVC和EntityFramework,使用的VS是2013版的,在Nuget的GUI界面下安装了EntityFramework(默认安装最新版的,怎么安装指定版本还没找到),按照MVC的示例项目MusicStore逐步做的过程中发现MVC4不支持EntityFramwok 6。

尝试去更新MVC版本各种失败,只能试着去降低EntityFramework版本。但是通过Nuget的GUI界面无法安装或更新到指定版本的EntityFramwok。在网上搜了搜相关资料(事实上也很容易搜到),做个备忘。

首先打开程序包管理器控制台:工具→Nuget程序包管理器→程序包管理器控制台

 

打开如下图所示的命令行界面

 

安装指定版本的Package(例如:EntityFramework 5.0):

PM> Install-Package EntityFramework -ProjectName MusicStore -Version 5.0

更新到指定版本的Package(例如EntityFramewrok 6.0):

PM> Update-Package EntityFramework -ProjectName MusicStore -Version 6.0
Update-Package命令相关参数说明: 

Parameter

Type

Description

-Id

string

Specifies the Id of the package to be updated.

-ProjectName

string

Specifies the name of the project in which packages should be updated.

-Version

SemanticVersion*

Specifies the new target version of the package as a result of the update.

-Source

string

Specifies where to look for package updates, overriding the package sources that are specified in the Options dialog. This value can be either a url to a remote repository or a path to a local repository or the name of a package source specified in the Options dialog.

-IgnoreDependencies

Switch parameter

If set, NuGet will ignore dependency packages and only update the main package.

-Safe

Switch parameter

If set, NuGet will only update to a new version that has the same major and minor versions as the previous package. For example, if the old version is 1.2.0, NuGet will accept the update package with version of 1.2.1 or 1.2.9999 but it will not accept 1.3.0.

-IncludePrerelease

Switch parameter

If set, NuGet will consider prerelease packages as candidates for updates.

-Reinstall

Switch parameter

If set, instead of updating the package to the latest version, NuGet will uninstall the package and reinstall the same version. This is useful when, for example, you‘ve updated the target framework of your project, e.g. from .NET 4.0 to .NET 4.5, and you want to reference .NET 4.5-specific assemblies in the package. You can‘t set this parameter together with the -Version parameter.

                              参考资料:http://docs.nuget.org/docs/reference/package-manager-console-powershell-reference

基本语法if(data){...} 

!data状态为空 或者undefined 或者false 都会执行下面的方法

data 就不会继续执行下面的方法

 

storage事件

storage还提供了storage事件,当键值改变或者clear的时候,就可以触发storage事件,如下面的代码就添加了一个storage事件改变的监听:

if(window.addEventListener){      window.addEventListener("storage",handle_storage,false); }else if(window.attachEvent){      window.attachEvent("onstorage",handle_storage); } function handle_storage(e){      if(!e){e=window.event;}         }

storage事件对象的具体属性如下表:

Property

Type

Description

key

String

The named key that was added, removed, or moddified

oldValue

Any

The previous value(now overwritten), or null if a new item was added

newValue

Any

The new value, or null if an item was added

url/uri

String

The page that called the method that triggered this change

 

博客园,汤姆大叔[Eval()用法参考]

使用新的Function()构造就类似于eval(),应小心接近。这可能是一个强大的构造,但往往被误用。如果你绝对必须使用eval(),你 可以考虑使用new Function()代替。有一个小的潜在好处,因为在新Function()中作代码评估是在局部函数作用域中运行,所以代码中任何被评估的通过var 定义的变量都不会自动变成全局变量。另一种方法来阻止自动全局变量是封装eval()调用到一个即时函数中。

考虑下面这个例子,这里仅un作为全局变量污染了命名空间。

console.log(typeof un);    // "undefined"
console.log(typeof deux); // "undefined"
console.log(typeof trois); // "undefined"

var jsstring = "var un = 1; console.log(un);";
eval(jsstring); // logs "1"

jsstring = "var deux = 2; console.log(deux);";
new Function(jsstring)(); // logs "2"

jsstring = "var trois = 3; console.log(trois);";
(function () {
   eval(jsstring);
}()); // logs "3"

console.log(typeof un); // number
console.log(typeof deux); // "undefined"
console.log(typeof trois); // "undefined"

 

另一间eval()和Function构造不同的是eval()可以干扰作用域链,而Function()更安分守己些。不管你在哪里执行 Function(),它只看到全局作用域。所以其能很好的避免本地变量污染。在下面这个例子中,eval()可以访问和修改它外部作用域中的变量,这是 Function做不来的(注意到使用Function和new Function是相同的)。

(function () {
   var local = 1;
   eval("local = 3; console.log(local)"); // logs "3"
   console.log(local); // logs "3"
}());

(function () {
   var local = 1;
   Function("console.log(typeof local);")(); // logs undefined
}());

 

你可以会想到,在使用eval对JSON进行执行的时候,JSON字符串通常被包含在一个圆括号里:eval(‘(‘ + json + ‘)‘),这样做的原因就是因为分组操作符,也就是这对括号,会让解析器强制将JSON的花括号解析成表达式而不是代码块。

  try {
    { "x": 5 }; // "{" 和 "}" 做解析成代码块
  } catch(err) {
    // SyntaxError
  }
 
  ({ "x": 5 }); // 分组操作符强制将"{" 和 "}"作为对象字面量来解析

 

还有一点需要提醒一下,函数声明在条件语句内虽然可以用,但是没有被标准化,也就是说不同的环境可能有不同的执行结果,所以这样情况下,最好使用函数表达式:

  // 千万别这样做!
  // 因为有的浏览器会返回first的这个function,而有的浏览器返回的却是第二个

  if (true) {
    function foo() {
      return ‘first‘;
    }
  }
  else {
    function foo() {
      return ‘second‘;
    }
  }
  foo();

  // 相反,这样情况,我们要用函数表达式
  var foo;
  if (true) {
    foo = function() {
      return ‘first‘;
    };
  }
  else {
    foo = function() {
      return ‘second‘;
    };
  }
  foo();

 

 

我们知道,这个匿名函数调用返回的函数(带有标识符g的函数),然后赋值给了外部的f。我们也知道,命名函数表达式会导致产生多余的函数对象,而该对象与返回的函数对象不是一回事。所以这个多余的g函数就死在了返回函数的闭包中了,因此内存问题就出现了。这是因为if语句内部的函数与g是在同一个作用域中被声明的。这种情况下 ,除非我们显式断开对g函数的引用,否则它一直占着内存不放。

  var f = (function(){
    var f, g;
    if (true) {
      f = function g(){};
    }
    else {
      f = function g(){};
    }
    // 设置g为null以后它就不会再占内存了
    g = null;
    return f;
  })();

 

JavaScript笔记杂谈篇(啥都有)

标签:

原文地址:http://www.cnblogs.com/workstation-liunianguowang/p/5189355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!