简体   繁体   English

.prop的JQuery typescript定义?

[英]JQuery typescript definition for .prop?

I have the following code: 我有以下代码:

// Populate the title of the selects on change and trigger a change
$('#modal .update-title')
    .change(function () {
        var $select = $(this),
        match = null,
        matchRow = null,
        title = $('option:selected', this).prop('title');

In typescript this sets the value of title to a bool. 在typescript中,它将title的值设置为bool。 Then when I try and use it as follows I am getting an error: 然后,当我尝试按如下方式使用它时,我收到一个错误:

$("#modal_Title_" + matchRow).val(title);

Is this another problem with the JQuery definition or something that I am doing wrong? 这是JQuery定义的另一个问题还是我做错的事情? When a check the definition file I see the following: 检查定义文件时,我看到以下内容:

prop(propertyName: string): bool;

This does not seem to match the JQuery doc. 这似乎与JQuery doc不匹配。

The jQuery documentation is incorrect, and so is your type definition file. jQuery文档不正确,您的类型定义文件也是如此。

Most properties are strings, but some are numbers (eg .selectedIndex ) and some are booleans ( .checked etc), and $.fn.prop() does not convert them all to strings. 大多数属性是字符串,但有些是数字(例如.selectedIndex ),有些是布尔值( .checked等),而$.fn.prop() 不会将它们全部转换为字符串。

The correct definition should be: 正确的定义应该是:

prop(propertyName: string): any;

There's already a work item listed on the TypeScript site to fix this. TypeScript网站上已列出一个工作项来解决此问题。

As @Alnitak pointed out, the return value should be 'any'. 正如@Alnitak指出的那样,返回值应该是'any'。

I have fixed the jQuery definitions file at DefinitelyTyped which is a fork from TypeScript's file, but with numerous fixes. 我已经修复了DefinitelyTyped的jQuery定义文件,这是一个来自TypeScript文件的分支,但是有很多修复。

The jQuery docs state that prop returns: jQuery文档声明prop返回:

  • a string when called with one argument 使用一个参数调用时的字符串

  • a jQuery object when called with two arguments 使用两个参数调用时的jQuery对象

http://api.jquery.com/prop/ http://api.jquery.com/prop/

So according to this the jquery.d.ts may be incorrect. 所以根据这个,jquery.d.ts可能是不正确的。

My recommendation would be to edit the jquery.d.ts to be correct in your project and raise a bug at typescript.codeplex.com 我的建议是在你的项目中编辑jquery.d.ts是正确的,并在typescript.codeplex.com上引发一个错误

prop(propertyName: string): string;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM