[英]How can i use this jquery function in my angular project
What my function does is, when i click on a text, it appends the same text below the clicked text. 我的功能是,当我单击文本时,它将在单击的文本下方附加相同的文本。 Here is the code below: 这是下面的代码:
$('*').on('click', function(e){ e.stopPropagation() var thisDiv_name = $(this).attr('id'); var thisDiv = $(this); var parentDiv = $(this).parent(); // $(this).clone().appendTo(parentDiv); $(this).attr('style','outline: #6c6b69 solid thin;'); $(this).clone().insertAfter(thisDiv); //alert($(this).html()); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <div id="main"> <div id="content">content</div> <div id="content2"> <p>This is p </p> <h3> This is h3 </h3> </div> <button id="button">show it</button> </div>
I want to achieve the same in my angular2 project 我想在我的angular2项目中实现相同的目标
I have called jquery on index.html. 我在index.html上调用了jquery。 Below is my html code [which is eactly same] 以下是我的html代码[完全相同]
test.component.html
<div id="main">
<div id="content">content</div>
<div id="content2">
<p>This is p </p>
<h3> This is h3 </h3>
</div>
<button id="button">show it</button>
</div>
My test.component.ts
我的test.component.ts
import { Component, OnInit } from '@angular/core';
declare var $: any;
@Component({
selector: 'app-contextmenu',
templateUrl: './contextmenu.component.html',
styleUrls: ['./contextmenu.component.css']
})
export class ContextmenuComponent implements OnInit {
constructor() { }
ngOnInit() {
}
$('*').on('click', function(e){
e.stopPropagation()
var thisDiv_name = $(this).attr('id');
var thisDiv = $(this);
var parentDiv = $(this).parent();
// $(this).clone().appendTo(parentDiv);
$(this).attr('style','outline: #6c6b69 solid thin;');
$(this).clone().insertAfter(thisDiv);
//alert($(this).html());
});
}
But it is not working and i am getting errors, please help me get this code working. 但是它无法正常工作,并且出现错误,请帮助我使此代码正常工作。
Edit 1: Errors coming - 编辑1:错误即将出现-
[ts] Parameter declaration expected. [ts]预期参数声明。 (property) (属性)
ContextmenuComponent['*']: any ContextmenuComponent ['*']:任意[ts] Parameter declaration expected. [ts]预期参数声明。 (property) (属性)
ContextmenuComponent['click']: any ContextmenuComponent ['click']:任意
my page.component.ts
code which works - 我的page.component.ts
代码有效-
import { Component, OnInit } from '@angular/core';
//import $ from 'jquery';
declare var $: any;
@Component({
selector: 'app-contextmenu',
templateUrl: './contextmenu.component.html',
styleUrls: ['./contextmenu.component.css']
})
export class ContextmenuComponent implements OnInit {
constructor() { }
ngOnInit() {
}
ngAfterViewInit(){
$(document).ready(function(){
$('*').on('click', function(e){
e.stopPropagation()
var thisDiv_name = $(this).attr('id');
var thisDiv = $(this);
var parentDiv = $(this).parent();
$(this).attr('style','outline: #6c6b69 solid thin;');
$(this).clone().insertAfter(thisDiv);
});
});
}
}
You need to call it like this - 您需要这样称呼它-
ngAfterViewInit(){
$(document).ready(function(){
...
} // your code
}
Web coding has become a bit different with angular 2. Spend some time and you will learn more. Web编码与angular 2有所不同。花一些时间,您将学到更多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.