小编geo*_*org的帖子

获取计算样式并省略默认值

我正在尝试获取元素的当前运行时样式并过滤掉具有默认值的属性.例如,使用这样的标记:

<style>
    .foo { background: red }
    span { font-size:30px }
</style>


<div style="color: blue">
    <span id="bar" class="foo">hello</span>
</div>
Run Code Online (Sandbox Code Playgroud)

我希望结果如下:

 background-color: red;
 color: blue;
 font-size: 30px;
Run Code Online (Sandbox Code Playgroud)

我试过window.getComputedStyle,但它返回了很多东西,我不确定如何过滤掉默认值.任何指针将不胜感激.

javascript css

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

将 JSX 字符串动态编译为组件

我有一个 React 应用程序,我需要在其中显示一个 HTML 弹出窗口,其中包含服务器生成的一些动态内容。此内容还包含一些 JSX 标记,我希望将其呈现为真实组件。

<MyButton onClick={displayInfo}/>

...

async displayInfo() {
    let text = await fetch(...)  

    console.log(text) // "<SomeComp onClick={foo}><OtherComp..... etc

    let component = MAGIC(text)

    ReactDom.render(component, '#someDiv')
}
Run Code Online (Sandbox Code Playgroud)

由于我的服务器应用程序的结构,ReactServerDOM 和 Hydration 不是一个选项。

有没有办法只在客户端实现这个?

javascript reactjs server-side-rendering

9
推荐指数
1
解决办法
1304
查看次数

Given a data matrix, calculate html rowspan and colspan

I have a sparse matrix like below consisting of data cells (1..9) and empty cells (=zero):

[
    [ 1, 2, 0, 3 ],
    [ 0, 4, 0, 0 ],
    [ 5, 6, 7, 8 ],
]
Run Code Online (Sandbox Code Playgroud)

I'd like to display this as an html table, but there should be no empty cells - they should be "covered" by their neighbouring data cells' row- and colspans:

<table border=1 cellpadding=10>
    <tr>
        <td rowspan=2>1</td>
        <td colspan=2>2</td>
        <td>3</td>
    </tr>
    <tr>
        <td colspan=3>4</td>
    </tr>
    <tr> …
Run Code Online (Sandbox Code Playgroud)

javascript

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

为IN列表中的缺失值返回NULL

我有这样一张桌子:

 id | val
 ---------
  1 | abc
  2 | def
  5 | xyz
  6 | foo
  8 | bar
Run Code Online (Sandbox Code Playgroud)

和查询一样

 SELECT id, val FROM tab WHERE id IN (1,2,3,4,5)
Run Code Online (Sandbox Code Playgroud)

返回

 id | val
 ---------
  1 | abc
  2 | def
  5 | xyz
Run Code Online (Sandbox Code Playgroud)

有没有办法让它返回NULL丢失的id,即

 id | val
 ---------
  1 | abc
  2 | def
  3 | NULL
  4 | NULL
  5 | xyz
Run Code Online (Sandbox Code Playgroud)

我想应该有一个棘手的LEFT JOIN与它本身,但不能包围我的头.

编辑:我看到人们正在考虑我想在序列中"填补空白",但实际上我想要的是将NULL替换为IN列表中的缺失值.例如,这个

 SELECT id, val FROM tab WHERE id IN (1,100,8,200)
Run Code Online (Sandbox Code Playgroud)

应该回来

 id …
Run Code Online (Sandbox Code Playgroud)

mysql sql

6
推荐指数
1
解决办法
692
查看次数

在原始字符串中尾随斜杠

只是一个快速愚蠢的问题.如何在原始字符串文字中编写尾部斜杠?

r = r'abc\'  # syntax error
r = r'abc\\' # two slashes: "abc\\"
Run Code Online (Sandbox Code Playgroud)

python syntax

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

将过滤器应用于自动连接的表

这是我的 SQL 设置

    create table a
    (
        id serial primary key,
        ta text
    );
    create table b
    (
        id serial primary key,
        tb text,
        aid integer references a(id) not null
    );
Run Code Online (Sandbox Code Playgroud)

Python:

import sqlalchemy as sa
import sqlalchemy.orm

connection_url = "..."
engine = sa.create_engine(connection_url, echo=True, future=True)
mapper_registry = sa.orm.registry()

class A:
    pass


class B:
    pass


mapper_registry.map_imperatively(
    B,
    sa.Table(
        'b',
        mapper_registry.metadata,
        sa.Column('id', sa.Integer, primary_key=True),
        sa.Column('tb', sa.String(50)),
        sa.Column('aid', sa.ForeignKey('a.id')),
    ))

mapper_registry.map_imperatively(
    A,
    sa.Table(
        'a',
        mapper_registry.metadata,
        sa.Column('id', sa.Integer, primary_key=True),
        sa.Column('ta', sa.String(50))
    ),
    properties={ …
Run Code Online (Sandbox Code Playgroud)

python postgresql sqlalchemy

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

可迭代类的类型注释

我有一个扩展的类ElementTree.Element

import xml.etree.ElementTree as ET
from typing import cast


class MyElement(ET.Element):
    def my_method(self):
        print('OK')


xml = '''<test> <sub/> <sub/> </test>'''

root: MyElement = cast(
    MyElement,
    ET.fromstring(xml, parser=ET.XMLParser(target=ET.TreeBuilder(element_factory=MyElement))))

root.my_method()  # this is fine

for ch in root:
    ch.my_method()  # PyCharm error message  ???
Run Code Online (Sandbox Code Playgroud)

这确实有效,但是 PyCharm 突出显示了最后一行,因为它认为chElement,而不是MyElement

我应该如何注释MyElement才能清楚地表明,当我迭代它时,我得到的是MyElement实例而不是ET.Elements?

python typing

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