0
var testvar = "something";

//only one of the following 3 lines were uncommented while testing:
var myinsert = '<span class="green myCursor" 
                   onClick="getproblemsadmin(testvar)">
                   Test
                </span>'; //doesn't pass testvar to function

var myinsert = '<span class="green myCursor" 
                   onClick="getproblemsadmin(\'testvar\')">
                   Test
                </span>'; //doesn't pass testvar to function

var myinsert = "<span class='green myCursor' 
                   onClick='getproblemsadmin(\"testvar\")\'>
                   Test
                </span>"; //doesn't pass testvar to function

$('.someclass').html(myinsert); //jquery works and this works (tested)

有没有办法将变量传递给这样的函数?

4

3 回答 3

2

你的意思是这个变量的值吗?

var myinsert = "<span class='green myCursor' onClick='getproblemsadmin(\""+testvar+"\")\'>Test</span>";

语法:

var myinsert = "string" + variable + "string";
var myinsert = 'string' + variable + 'string';
于 2012-07-14T13:08:27.743 回答
1

您可以尝试创建 span 元素,然后绑定 click 事件:

$('<span class="green myCursor">Test</span>').click(function()
{
    getproblemsadmin('testvar')
});

或者,如果你想获得真正的 jQuery'esk,你可以这样写:

$('<span />').addClass('green myCursor').text('test').click(function()
{
    getproblemsadmin('testvar')
});
于 2012-07-14T13:24:54.913 回答
0

如果我理解正确,您正在寻找一种将变量传递给函数的方法,该函数将返回包含标记代码的构造字符串?像这样的东西?

var testvar = "something";

var myinsert = work_horse(testvar);
$('.someclass').html(myinsert);

function work_horse(testvar){
    return '<span class="green myCursor" onClick="getproblemsadmin(' + testvar + ')">Test</span>';
}​
于 2012-07-14T13:12:03.700 回答