我想在Typescript/Reactjs中的HTML元素上触发click事件.
let element: Element = document.getElementsByClassName('btn')[0];
element.click();
Run Code Online (Sandbox Code Playgroud)
上面的代码确实有效.但是我收到了一个Typescript错误:
ERROR in [at-loader] ./src/App.tsx:124:17
TS2339: Property 'click' does not exist on type 'Element'.
Run Code Online (Sandbox Code Playgroud)
那么这样做的正确方法是什么?
我正在使用Vanilla JS中的对话脚本.我遇到了视频图像上的点击事件的问题.即使是艰难的图像被锚标记包围,它也会在"trigger-dialog-open"事件中将图像显示为event.target.
这是HMTL:
<a class="trigger-dialog--open thumbnail" data-dialog-id="dialog-video" href="javascript:;">
<figure>
<img src="http://i.ytimg.com/vi/id/sddefault.jpg" alt="" />
</figure>
</a>
Run Code Online (Sandbox Code Playgroud)
这是JS中的事件:
var openTriggers = document.getElementsByClassName('trigger-dialog--open');
for (var i = 0; i < openTriggers.length; i++) {
openTriggers[i].addEventListener("click", function (event) {
this.openDialog(event.target.getAttribute('data-dialog-id'));
}.bind(this), false);
}
Run Code Online (Sandbox Code Playgroud)
事件处理程序想要知道anchors数据属性中的dialog-id.无法找到它,因为它认为图像是event.target,而不是实际的锚点.我怎么能纠正这个?谢谢!
在这个反应应用程序中,有一个带有几个输入字段的表单.这些字段都this.handleChange与onChange属性一起使用.
private handleChange = (event: React.FormEvent<HTMLInputElement>) => {
let target = event.target as HTMLInputElement;
this.setState({
[target.name]: target.value
});
};
Run Code Online (Sandbox Code Playgroud)
我得到的打字稿错误是:
ERROR in [at-loader] ./src/components/FormSubmitHighScore.tsx:43:23
TS2345: Argument of type '{ [x: string]: string; }' is not assignable to parameter of type 'Pick<State, "first_name" | "last_name" | "email_address" | "school_name" | "region" | "submitted">'.
Property 'first_name' is missing in type '{ [x: string]: string; }'.
Run Code Online (Sandbox Code Playgroud)
因此,通过动态设置状态名称字段(基于正在使用的输入),会触发此Typescript错误(但它在浏览器中可以正常工作).
如何在此上下文中指定正确的类型?
在 WordPress 的后端,我使用默认http://localhost/sitename/example-post/值来创建永久链接。
对于自定义帖子类型,我以这种方式定义了自定义 slug,例如services:
register_post_type( 'service',
array(
'labels' => array(
'name' => __( 'Services' ),
'singular_name' => __( 'Service' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array(
'slug' => 'services',
'with_front' => true
),
'supports' => array(
'title',
'editor',
'excerpt',
'thumbnail'
),
'taxonomies' => array( 'category' ),
)
);
Run Code Online (Sandbox Code Playgroud)
它创建services/post-name。
我还使用此挂钩创建自定义页面来创建自定义页面永久链接:
function custom_base_rules() {
global $wp_rewrite;
$wp_rewrite->page_structure = $wp_rewrite->root . '/page/%pagename%/';
}
add_action( 'init', 'custom_base_rules' );
Run Code Online (Sandbox Code Playgroud)
它创建页面/帖子名称
现在我唯一需要做的就是为正常的 WordPress …
php wordpress permalinks wordpress-theming custom-wordpress-pages
我尝试了很多不同的功能和方法,但到目前为止我还无法让它工作。目标是使用一些 PHP 代码将高级自定义字段组添加到 Wordpress 的后端。在最好的情况下,我们将 PHP 代码添加到类的方法中。
public function create_group( $group_name ) {
if ( $this->does_group_already_exists( $group_name ) ) {
return false;
}
acf_add_local_field_group( array(
'key' => 'group_1',
'title' => 'My Group',
'fields' => array(
array(
'key' => 'field_1',
'label' => 'Sub Title',
'name' => 'sub_title',
'type' => 'text',
)
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
),
),
) );
return true;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码没有添加任何内容。我还尝试将它添加到functions.php它并使用add_action()如下函数:
add_action( 'acf/init', …Run Code Online (Sandbox Code Playgroud) 我想使用import语句将原型化的 JavaScript 对象加载到我的 Reactjs 项目中。
首先,我有一个原型 Javascript 对象,我将其导出为 Webpack 模块:
var FloDialog=function(t){this.id=Date.now(),this.cloak=this.rende // etc //etc
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = FloDialog;
}
Run Code Online (Sandbox Code Playgroud)
然后在类型定义文件中flo-dialog.min.d.ts我有您在下面看到的代码:
declare module 'flo-dialog' {
interface FloDialogOptions {
cache: boolean;
position: string;
effect: {
fade: boolean;
fill: boolean;
}
}
interface FloDialog {
constructor(opts?: FloDialogOptions): void;
openUrl(title: string, url: string, callback?: () => void): void;
}
const FloDialog: FloDialog;
export = FloDialog;
}
Run Code Online (Sandbox Code Playgroud)
import * as …
我刚开始使用 Laravel Collective Form Service Provider/FormBuilder。
我使用这个自定义组件来渲染带有标签的文本字段。问题是我试图用 __() 函数翻译它的标签,但是 $name 变量在非常晚的阶段从 first_name 转换为 First Name。
<div class="control-group">
{{ Form::label($name, null, ['class' => 'group__label']) }}
{{ Form::text($name, $value, array_merge(['class' => 'control-
group__control'], $attributes)) }}
</div>
Run Code Online (Sandbox Code Playgroud)
我不能简单地这样做:
{{ Form::label(__($name), null, ['class' => 'group__label']) }}
Run Code Online (Sandbox Code Playgroud)
同样,因为它获得 first_name,然后将其转换为 First Name。我的 nl.json 文件包含 First Name 的翻译,而不是 first_name。
如果我将 _() 转换函数添加到 FormBuilder 中标签方法的最后一条规则,那么它解决了我的问题。但是,当然,我不想修改供应商代码!
return $this->toHtmlString('<label for="' . $name . '"' . $options . '>' . __($value) . '</label>');
Run Code Online (Sandbox Code Playgroud)
所以。如何解决这个问题?不知何故,我需要创建一个自定义的 FormBuilder->label() 方法吗?
问题是在我的一个寻呼机网站上没有填充标题标签。
page.php 看起来像这样:
<?php
locate_template( 'page-home.php', true );
exit();
Run Code Online (Sandbox Code Playgroud)
page-home.php 看起来像这样:
<?php get_header(); ?>
<?php get_template_part('template-sections/slider'); ?>
<?php get_template_part('template-sections/services'); ?>
<?php //etc. ?>
<?php get_footer();
Run Code Online (Sandbox Code Playgroud)
header.php 看起来像这样:
<!doctype html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="profile" href="http://gmpg.org/xfn/11"/>
<base href="<?php echo get_site_url(); ?>/">
<?php wp_head(); ?>
</head>
<body itemprop="hasPart" itemscope="" itemtype="http://schema.org/WebPage" <?php body_class(); ?>>
<!-- etc -->
Run Code Online (Sandbox Code Playgroud)
而且footer.php是这样的:
<?php wp_footer(); ?>
</main>
<aside class="aside">
<div class="aside__content">
</div>
</aside>
</div> …Run Code Online (Sandbox Code Playgroud) javascript ×4
php ×4
reactjs ×3
typescript ×3
wordpress ×3
forms ×1
laravel ×1
permalinks ×1
seo ×1