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)
有没有办法将变量传递给像这样的函数?
答案 0 :(得分:2)
你的意思是这个变量的值吗?
var myinsert = "<span class='green myCursor' onClick='getproblemsadmin(\""+testvar+"\")\'>Test</span>";
语法:
var myinsert = "string" + variable + "string";
var myinsert = 'string' + variable + 'string';
答案 1 :(得分: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')
});
答案 2 :(得分: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>';
}