小编Mik*_*lik的帖子

取消绑定特殊按键事件

我有一个关于jQuery按键事件的问题.我有以下(工作)代码:

$(document).bind('keypress', function(event) {

    if ($('#myDiv').is(':visible')) {

        if (event.which == 102) {
            // ...do something...
        }

    }
    else {
        if (event.which == 102) {
            return;
        }
    }

});
Run Code Online (Sandbox Code Playgroud)

我总是"绑定"事件,绑定另一个"结束"它.我知道我可以解开它,.unbind('keypress')但是我得到了更多的按键事件,当我解开这个时,我的$(document).unbind('keypress')所有事件都会丢失.

我可以做一些像"keypress.102"这样的东西来解开这个特定的"密钥",或者怎么办呢?!

jquery keypress unbind

12
推荐指数
1
解决办法
1万
查看次数

jQuery:动画文本颜色

我想动态更改悬停事件中的链接颜色.到目前为止,我得到了以下代码,但它不起作用.有什么建议吗?在我看来,这似乎是正确的......

    $('.fadelink').hover(function(){            
        $(this).animate({
            color: '#333'
        }, 600);            
    },
    function(){
        $(this).animate({
            color: '#999'
        }, 600);          
    });
Run Code Online (Sandbox Code Playgroud)

jquery hyperlink jquery-animate

7
推荐指数
1
解决办法
2万
查看次数

Angular http get 与异步等待

我的服务组件中有以下代码,它从 api 获取数据:

async getIngredientsByProductSubCategory(productSubCategoryId: number) {
    const url = '/my-url';
    let dataToReturn: any;
    await this.http.get(url).toPromise()
        .then(data => {
            console.log(data);
            dataToReturn = data;
        });
    return dataToReturn;
}
Run Code Online (Sandbox Code Playgroud)

问题是上面的 console.log 输出正确的数据,但是当我从另一个组件执行它时,如下所示:

this.myService.getIngredientsByProductSubCategory(4);
Run Code Online (Sandbox Code Playgroud)

我从 console.log 中得到以下输出:

“ZoneAwarePromise”类型的日志数据

我需要做什么才能在其他组件中获取正确的数据?我必须以任何方式解决这个问题吗?

更新:

我的工作解决方案:

服务:

getIngredientsByProductSubCategory(productSubCategoryId: number) {
    const url = '/my-url';
    return this.http.get(url).toPromise();
}
Run Code Online (Sandbox Code Playgroud)

成分:

async getIngredients() {
    const ingredientsByProductSubCategory = await this.frontendService.getIngredientsByProductSubCategory(4);
}
Run Code Online (Sandbox Code Playgroud)

我还读到,如果您没有消耗重复数据的要求,则可能不会使用 Observables。

感谢大家的帮助!

async-await angular

6
推荐指数
2
解决办法
2万
查看次数

手风琴与外容器配对

我对手风琴有以下问题.我有一对外部容器"accordion"的元素,但我需要用另一个容器包装每一对.据我所知,我之前无法将它们包起来,因为手风琴不能正常工作......所以我需要在domready之后用一个额外的片段包装它们......

我懂了:

<div id="accordion">
    <h2 class="head">Headline</h2>
    <div class="content">Some content...</div>
    <h2 class="head">Headline</h2>
    <div class="content">Some content...</div>
 ....more pairs
</div>
Run Code Online (Sandbox Code Playgroud)

我需要这个:

<div id="accordion">
<div class="outer">
    <h2 class="head">Headline</h2>
    <div class="content">Some content...</div>
</div>
<div class="outer">
    <h2 class="head">Headline</h2>
    <div class="content">Some content...</div>
</div>
...more pairs
</div>
Run Code Online (Sandbox Code Playgroud)

我以为这会做的工作:

$('.head').before('<div class="outer">');
$('.content').after('</div>'); 
Run Code Online (Sandbox Code Playgroud)

...但它在每个标题之前插入已经关闭的div.有人可以帮忙吗?

javascript jquery accordion

2
推荐指数
1
解决办法
615
查看次数