Html 在动态创建的元素的onclick 函数的属性中传递字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16035617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:36:09  来源:igfitidea点击:

Passing a string in onclick function's attribute of element dynamically created element

javascripthtml

提问by Mohamed Hussain

I am trying to pass a string in the onClick event handler function's arguments of the dynamically created anchor element, see the fiddle http://jsfiddle.net/shmdhussain/bXYe4/.

我试图在动态创建的锚元素的 onClick 事件处理程序函数的参数中传递一个字符串,请参阅小提琴http://jsfiddle.net/shmdhussain/bXYe4/

I am not able to pass the string to the function, but i am able to pass the number integer to the function. Please help me in this. Thanks in advance.

我无法将字符串传递给函数,但我能够将数字整数传递给函数。请帮助我。提前致谢。

html:

html:

<DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>test</title>
        <link rel="stylesheet" href="mystyle.css" class="cssfx"/>

        <script src="/jquery.min.js"></script>
        <script src="colon.js"></script>
    </head>

    <body>

        <div class="mytest">

        </div>


    </body>
</html>
</html>

Javascript:

Javascript:

var elem=[  {"name":"husain","url":"http://google.com","age":21},
            {"name":"ismail","url":"http://yahoo.com","age":22},
            {"name":"nambi","url":"http://msn.com","age":23}
         ]

jQuery(function($){
    var str="";
    for(i=0;i<elem.length;i++){
        str+="<a href='#' onclick='test('"+elem[i].url+"')'>dd</a><br><br>"
        console.log(str);

    }
    $('.mytest').html(str);

});

function test(url){
    console.log("url is "+url);
}

回答by AmShaegar

You have to use proper string sytax. This

您必须使用正确的字符串语法。这个

"<a href='#' onclick='test('"+elem[i].url+"')'>dd</a><br><br>"

will result in

会导致

<a href='#' onclick='test('http://domain.tld')'>dd</a><br><br>

You cannot use ' for onclick and the parameters of test. Use \" instead.

您不能将 ' 用于 onclick 和测试参数。改用\"。

"<a href='#' onclick='test(\""+elem[i].url+"\")'>dd</a><br><br>"

Which results in

这导致

<a href='#' onclick='test("http://domain.tld")'>dd</a><br><br>

回答by Sébastien Garmier

Your problem is, that str+="<a href='#' onclick='test('"+elem[i].url+"')'>dd</a><br/><br/>";will return a string like "<a href='#' onclick='test('your_url')'>dd</a><br/><br/>". This will generate a html like this:

你的问题是,这str+="<a href='#' onclick='test('"+elem[i].url+"')'>dd</a><br/><br/>";将返回一个像"<a href='#' onclick='test('your_url')'>dd</a><br/><br/>". 这将生成一个像这样的 html:

<a href='#' onclick='test('your_url')'>dd</a><br/><br/>

In this case, the onclick attribute contains only 'test('.

在这种情况下, onclick 属性仅包含 'test('.

Try this:

尝试这个:

str+="<a href='#' onclick='test(\""+elem[i].url+"\")'>dd</a><br/><br/>";

This will generate a html like this:

这将生成一个像这样的 html:

<a href='#' onclick='test("your_url")'>dd</a><br/><br/>

回答by Surama Hotta

Directly use:

直接使用:

test('"+elem[i].url+"') 

instead of:

代替:

'test('"+elem[i].url+"')' 

to pass the string inside a method.

在方法中传递字符串。

回答by Ahmet DAL

If I didn't misunderstand your question, here is your answer;

如果我没有误解你的问题,这是你的答案;

var elem=[  {"name":"husain","url":"http://google.com","age":21},
    {"name":"ismail","url":"http://yahoo.com","age":22},
    {"name":"nambi","url":"http://msn.com","age":23}
]

jQuery(function($){
    var str="";
    for(i=0;i<elem.length;i++){
        var a =$('<a>',{href:'#',text:'dd'});
        a.click(function(e){
               test(elem[i].url)
        });
        $('.mytest').append(a).append($('</br>')).append($('</br>'));

    }
});

function test(url){
    console.log("url is "+url);
}

回答by PVIJAY

For passing multiple parameters you can cast the string by concatenating it with the ASCII value like, for single quotes we can use &#39;

要传递多个参数,您可以通过将字符串与 ASCII 值连接来转换字符串,例如,对于单引号,我们可以使用 &#39;

var str= "&#39;"+ str+ "&#39;";

the same parameter you can pass to the onclick(str)event.It is helpful in cases where we pass multiple parameters.It works with every browser.

您可以传递给事件的相同参数onclick(str)。在我们传递多个参数的情况下很有帮助。它适用于每个浏览器。

回答by priti

Directly use:

直接使用:

test('elem[i].url') 

instead of:

代替:

test('"+elem[i].url+"')

to pass the string inside a method.

在方法中传递字符串。

回答by chiranjeeb

var funcName = "SelectPickerBtnSelectEvent('" + str + "')";
$(".btn.dropdown-toggle.btn-light").attr("onclick", funcName);

回答by Jasen

perhaps instead, use a data attribute on the element to hold the string. then in the function retrieve the data.

也许相反,使用元素上的数据属性来保存字符串。然后在函数中检索数据。