js动态添加html标签函数中的参数写法

js动态添加html标签,此html中包含onclick等事件,必然要引用函数,那么函数中的参数如何写?

1,function showi(m){
alert(m);
}
function change(){
var  x=6;
document.write('<a href="#"  οnclick="showi(\'  '+x+'   \')">点击</a>');
};
change();

//6

2,document.write('<a href="#"  οnclick="showi( '+x+'  )">点击</a>');

//6

3,document.write('<a href="#"  οnclick="showi(  x  )">点击</a>');

//出错

4,document.write('<a href="#"  οnclick=" x=7; showi(\'  '+x+'   \')">点击</a>');

//6

5,document.write('<a href="#"  οnclick=" x=7; showi(   '+x+'   )">点击</a>');

//6

6,document.write('<a href="#"  οnclick=" x=7; showi(   x   )">点击</a>');

//7

7,document.write('<a href="#"  οnclick=" x=\'hello\'; showi(   x   )">点击</a>');

//hello

8,var  x="HELLO";

document.write('<a href="#"  οnclick="  showi(\' '+   x +' \'   )">点击</a>');

//HELLO;

9, var x=6;

document.write('<a href="#"  οnclick="  showi(\' +   x + \'   )">点击</a>');

//+x+

10,var x=6

document.write('<a href="#"  οnclick="  showi('+   x +'    )">点击</a>');

// 6

根据以上实验:

在js动态添加html标签,此html中包含onclick等事件,引用函数,那么函数中的参数这样写:

当函数中的参数来自于标签中的变量,参数形式就是普通的形式,直接showi(  x  )就行了。

eg:document.write('<a href="#"  οnclick=" x=\'hello\'; showi(   x   )">点击</a>');

document.write('<a href="#"  οnclick="  showi(   'hello'   )">点击</a>');

document.write('<a href="#"  οnclick="  showi(  7  )  ">点击</a>');

当函数中的参数来自于动态加载html的js,若参数是数字,则showi( '+x+');若参数是字符串,则showi(\'  '+x' \');

 

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值