小编Rya*_*P13的帖子

将jQuery转换为原始JS-在h1之后插入p元素

关于如何将这个jQuery转换为香草JS的任何想法:

$('.section > h1').after('<p>This paragraph was inserted with jQuery</p>');
Run Code Online (Sandbox Code Playgroud)

我是jQuery的新手,甚至是香草JS的新手。

据我所知:

var newP = document.createElement('p');

var pTxt = document.createTextNode('This paragraph was inserted with JavaScript');

var header = document.getElementsByTagName('h1');
Run Code Online (Sandbox Code Playgroud)

不知道从这里去哪里?

javascript jquery

5
推荐指数
0
解决办法
4791
查看次数

Z-index:如何使嵌套元素出现在其父元素下面

这个小提琴应该证明这个问题:

http://jsfiddle.net/5sqxQ/2/

我希望子菜单显示在父菜单下面.我当时正在寻求用JavaScript扩展这个以在父li元素的悬停下从下面滑动菜单.

没有关于JavaScript的讨论,但无法弄清楚如何设置元素的样式以实现所需的分层.

css nested z-index

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

Cordova after_prepare hook在Android中成功但在iOS中失败

我为Cordova构建编写了一个after_prepare钩子,它从最终构建中删除了node_modules文件夹:

#!/usr/bin/env node

/**
 * The node modules we want to remove from the build prior to building
 * @type {Array}
 */
var foldersToRemove = ["platforms/android/assets/www/node_modules", "platforms/ios/www/node_modules"];

var fse = require('fs-extra');
var path = require('path');
var rootdir = process.argv[2];

foldersToRemove.forEach(function(folder) {

    var rmFolder = path.join(rootdir, folder);

    fse.remove(rmFolder, function(err) {
        if (err) {
            return console.error(err);
        } else {
            console.log(rootdir);
            console.log("fse folder removed success!")
        }
    });

});
Run Code Online (Sandbox Code Playgroud)

当我cordova prepare android -d在CLI中运行时,这对我有用,但在切换到iOS时,它失败并出现以下错误:

env:node\r:没有这样的文件或目录Hook失败,错误代码为127:

我试过只引用ios平台文件夹,它发出相同的错误消息.

android build node.js ios cordova

5
推荐指数
1
解决办法
3367
查看次数

使用可选属性扩展 TypeScript 类型

我正在使用 NextJS,需要使用AppProps我自己添加的可选参数来扩展它们的类型Layout。然而,该AppProps类型由许多其他类型组成,例如

应用程序属性:

export declare type AppProps<P = {}> = AppPropsType<Router, P>;
Run Code Online (Sandbox Code Playgroud)

应用程序属性类型:

export declare type AppPropsType<R extends NextRouter = NextRouter, P = {}> = AppInitialProps & {
    Component: NextComponentType<NextPageContext, any, P>;
    router: R;
    __N_SSG?: boolean;
    __N_SSP?: boolean;
};
Run Code Online (Sandbox Code Playgroud)

下一个组件类型:

export declare type NextComponentType<C extends BaseContext = NextPageContext, IP = {}, P = {}> = ComponentType<P> & {
    /**
     * Used for initial page load data population. Data returned from `getInitialProps` is serialized when …
Run Code Online (Sandbox Code Playgroud)

javascript types extend typescript

5
推荐指数
2
解决办法
6722
查看次数

期望在反应测试库中使用参数调用 jest mock 失败

我希望这个测试通过,但它失败了:

it('should consume files on drop', () => {
  const mock = jest.fn();
  const file = new File(['file'], 'file.txt', { type: 'text/plain' });
  const fileList = [file];

  render(<DropZone onDocumentDrop={mock} />);

  const dropZone = screen.getByTestId('dropZone');
  const dropEvent = createEvent.drop(dropZone);

  Object.defineProperty(dropEvent, 'dataTransfer', {
    value: {
      files: {
        item: (index: number) => fileList[index],
        length: fileList.length,
      },
    },
  });

  fireEvent(dropZone, dropEvent);

  expect(dropZone).toBeInTheDocument();
  expect(mock).toHaveBeenCalled();
  expect(mock).toHaveBeenCalledWith({
    item: (index: number) => fileList[index],
    length: 1,
  });
});
Run Code Online (Sandbox Code Playgroud)

Jest 报告说:

expect(jest.fn()).toHaveBeenCalledWith(...expected)

- Expected
+ Received

- {"item": [Function item], …
Run Code Online (Sandbox Code Playgroud)

file-upload mocking reactjs jestjs react-testing-library

5
推荐指数
1
解决办法
1216
查看次数

在 NextJS getServerSideProps 中使用 Bearer token 装饰 api 请求

我正在使用axios拦截器用标头来装饰我的所有请求Authorization

@auth0/nextjs-auth0我通过使用 API 路由获取令牌/api/token

import auth0 from '@/libs/auth0/auth0';
import { AccessTokenResponse } from '@auth0/nextjs-auth0/dist/tokens/token-cache';
import { NextApiRequest, NextApiResponse } from 'next';

export async function getAccessToken(
  req: NextApiRequest,
  res: NextApiResponse
): Promise<AccessTokenResponse> {
  const tokenCache = auth0(req).tokenCache(req, res);
  return tokenCache.getAccessToken();
}

export default async function token(
  req: NextApiRequest,
  res: NextApiResponse
): Promise<void> {
  try {
    const { accessToken } = await getAccessToken(req, res);
    res.status(200).end(accessToken);
  } catch (error) {
    res.status(error.status || 500).end(error.message);
  }
}
Run Code Online (Sandbox Code Playgroud)

api …

session jwt auth0 next.js

5
推荐指数
1
解决办法
5692
查看次数

jsFiddle使用echo测试jQuery AJAX请求

以下代码警告"未定义",并且不会像我预期的那样在响应数据中附加html.有谁知道为什么?

JavaScript的:

$(function() {

    $('.document').on('click', '.ajax', function(e) {
        e.preventDefault();

        // ajax request
        $.ajax({
            async: true,
            cache: false,
            type: 'post',
            url: '/echo/html/',
            data: {
                html: '<p>This is echoed the response in HTML format</p>',
                delay: 1
            },
            dataType: 'html',
            beforeSend: function() {
                console.log('Fired prior to the request');
            },
            success: function(data) {
                console.log('Fired when the request is successfull');
                $('.document').append(data);
            },
            complete: function() {
                console.log('Fired when the request is complete');
            }
        });

    });

});?
Run Code Online (Sandbox Code Playgroud)

HTML:

<div class="document">
    <a class="ajax" href="#">Fire an AJAX request</a>
</div>? …
Run Code Online (Sandbox Code Playgroud)

ajax jquery

4
推荐指数
1
解决办法
6852
查看次数

使用偏移量计算JavaScript日期时间

我正在经历一些GMT时区偏移钙化的意外结果.

我正在使用ExtJS日期类来帮助计算消息从GMT 0服务器到达用户本地机器(当前GMT +8)的时间.

我想如果我计算了偏移量,那么在几秒钟内将其添加到时间戳,然后我可以使用以下计算给我一个字符串格式化我喜欢.

var d = new Date((stamp + offset) * 1000);
Run Code Online (Sandbox Code Playgroud)

stamp是以秒为单位的日期/时间,也就是偏移量.

这将返回我所在位置的当前日期和时间,但加上8小时.如果我不添加偏移量,那么时间是正确的.

方法new Date()会自动给我当地时间的日期/时间吗?

javascript datetime extjs date

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

IE7和IE6中的jQuery .ajax方法不起作用,但在Firefox中工作正常

这与我以前的帖子有关:

jQuery .load方法导致页面刷新AJAX

我更改了我的implmentation以使用.ajax方法而不是.load,它在Firefox中工作正常,但在IE7或IE6中没有:

    $('ul#coverTabs > li > a').live('click', function(event) {

    // Find href of current tab
    var $tabValue = $(this).attr('href');

    $.ajax({
        type: "GET",
        cache: false,
        dataType: "html",
        url: $(this).attr('href'),
        success: function(data){

        $(data).find('.benefitWrap').each(function(){

            var $benefitWrap = $(this).html();

            $('.benefitWrap').replaceWith($('<div class="benefitWrap">' + $benefitWrap + '</div>'));

        });

       }

    });

    event.preventDefault(); 

});
Run Code Online (Sandbox Code Playgroud)

这让我很伤心,因为它已经花了很长时间才能实现这一目标.

我出错的任何想法?

ajax jquery internet-explorer-7 internet-explorer-6

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

jQuery从DOM中删除元素仍然报告存在

我有一个地址查找器系统,用户输入邮政编码,如果邮政编码被验证,然后返回并显示地址列表,他们然后选择一个地址行,列表消失,然后地址行被进一步拆分成某些表格输入.

我面临的问题是,当他们通过上述过程然后清除邮政编码表单字段,点击查找地址按钮并重新显示地址列表.

事件虽然列表和父tr已从DOM中删除但它仍然报告它以长度1存在?

我的代码如下:

jQuery的

// when postcode validated display box
var $addressList = $("div#selectAddress > ul").length;

// if address list present show the address list
if ($addressList != 0) {
    $("div#selectAddress").closest("tr").removeClass("hide");
}
// address list hidden by default
// if coming back to modify details then display address inputs
var $customerAddress = $("form#detailsForm input[name*='customerAddress']");

var $addressInputs = $.cookies.get('cpqbAddressInputs');

if ($addressInputs) {
    if ($addressInputs == 'visible') {
        $($customerAddress).closest("tr").removeClass("hide");
    }
} else {
    $($customerAddress).closest("tr").addClass("hide");
}
// Need to change form action …
Run Code Online (Sandbox Code Playgroud)

javascript jquery

3
推荐指数
1
解决办法
1908
查看次数