Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript onclick函数字符串出现问题_Javascript - Fatal编程技术网

javascript onclick函数字符串出现问题

javascript onclick函数字符串出现问题,javascript,Javascript,嘿,大家好,我已经试了几分钟这个问题,我似乎不知道如何纠正它。当函数中涉及多个变量时,我往往最难做这样的事情 代码如下: var tempString = "thePrompt('Are you sure you wish to delete this?', 'theDel('" + IDnum + "', '" + theTitle + "', \'" + temperDay + "\', \'" + temperMonth + "\', \'" + temperYear + "\', \

嘿,大家好,我已经试了几分钟这个问题,我似乎不知道如何纠正它。当函数中涉及多个变量时,我往往最难做这样的事情

代码如下:

  var tempString = "thePrompt('Are you sure you wish to delete this?', 'theDel('" + IDnum + "', '" + theTitle + "', \'" + temperDay + "\', \'" + temperMonth + "\', \'" + temperYear + "\', \'" + DDiff + "\')";

 html +=
"<div id='theTable" + IDnum + "' class='fc-event fc-event-hori fc-corner-left fc-corner-right' style='position:absolute; margin-top: 15px; z-index:8;left:"+left+"px'><div id='apDEL' style='position:relative;width:0px;height:0px;z-index:1000; top:-13px; left:2px; float:left;'><img src='img/xCal.png' width='15' height='15' alt='' border='0' onclick=\"" + tempString + "\" /></div>" + (ETC ETC...)
它指向“105”

和往常一样,任何帮助都会很好o)

大卫

编辑/更新

好的,现在我已经移动了一些东西,我让它工作了(那就是将值发送到弹出提示框,但由于某种原因,在我点击删除按钮后,它看不到我的函数!这是因为提示在主页上,而我正在使用的代码在iframe中)

function theDel(theID, theTitle, day, month, year, DDiff)
{
    var tempString = "delClientE(" + theID + ", '" + theTitle + "', " + day + ", " + month + ", " + year + ", " + DDiff + ")";
    parent.thePrompt('Are you sure you wish to delete this event?', tempString);
}
“删除”按钮值现在如下所示:

delClientE(110, 'sadfsadfsdf', 14, 3, 2010, 2); jQuery.prompt.close();
var tempString = "thePrompt('question', theDel(params)";
但是,即使放置parent.delClientE也找不到该函数…当提示框在iframe之外时,如何从iframe中调用它


大卫

我想这句话的结尾不见了。你的话是这样的:

delClientE(110, 'sadfsadfsdf', 14, 3, 2010, 2); jQuery.prompt.close();
var tempString = "thePrompt('question', theDel(params)";
…它应该在最后有一个“结束”)

还有一件事你可能想看一看,事实上del中的参数是用单引号括起来的

thePrompt('Are you sure you wish to delete this?', 'theDel('105', '50 points for visiting us today!!!!', '13', '3', '2010', '2')' )
这可能是一个问题,因为prompt中的参数也是用单引号括起来的,所以我会让prompt在它的参数周围有双引号,所以它看起来像这样:

var tempString = "thePrompt(\"Are you sure you wish to delete this?\", \"theDel(\'" + IDnum + "\', \'" + theTitle + "\', \'" + temperDay + "\', \'" + temperMonth + "\', \'" + temperYear + "\', \'" + DDiff + "\')\")";    

这是因为这里的引号
“theDel”(“+
)应该转义内部的单引号。
编辑。当然,除了表中已经存在的缺少右paren的问题之外:)

在嵌套上下文中跟踪转义是非常困难的,因为当您出错时,您经常会给自己带来脚本注入安全问题,因此最好通过将字符串粘在一起来避免创建HTML/JS

相反,使用DOM方法从JavaScript分配事件处理程序,并告别所有令人恼火的反斜杠和引用内容:

var img= document.createElement('img');
img.src= 'img/xCal.png';
img.width=img.height= 15;
img.onclick= function() {
    if (confirm('Are you sure you wish to delete this?'))
        theDel(IDnum, theTitle, temperDay, temperMonth, temperYear, DDiff);
};
div.appendChild(img);

David,我不确定这个问题是否得到了您满意的答案,但是为这个问题创建一个临时变量可能更容易:

  var theDel = "theDel('" + IDnum + "', '" + theTitle + "', '" + temperDay + "', '" + temperMonth + "', '" + temperYear + "', '" + DDiff + "')";
  var tempString = "thePrompt('Are you sure you wish to delete this?', \' + theDel + \')";

首先,你最好用它

如果做不到这一点,字符串插值函数会使这些事情变得更容易:

String.prototype.format = function(values) {
  return this.replace(/\{(.+?)\}/g, function(s, key) {
    return values[key];
  });
}
只要像这样的事情变成:

var s = 'blah blah blah "{IDnum}" blah blah blah \'{foosball}\''.format({ IDnum: '1', foosball: 'xyz'});
(如果你不喜欢操纵
字符串的原型,你可以把函数放在其他地方。)

好的,这个怎么样

iFrame源

<html>
<head>
<script type="text/javascript">
    function localDel(params){
        if (window.parent && typeof window.parent.thePrompt==='function')
            window.parent.thePrompt('Are you sure you wish to delete this event?', params);
    }
</script>
</head>
<body>
    <p onclick="localDel([1,'some title', 14, 3, 2010, 2])">delete</p>
    <p onclick="localDel([2,'another title', 14, 3, 2010, 2])">delete</p>
</body>
</html>
<html>
<head>
    <script type="text/javascript">

    function thePrompt(msg,params) {
        if(confirm(msg)) theDel.apply(this,params);
    }

    function theDel(id, title, tDay, tMonth, tYear, dDiff) {
      alert(id);
      // etc...
    }

    </script>
</head>
<body>
    <iframe src="iframe.html" />
</body>
</html>

函数localDel(params){
if(window.parent&&typeof window.parent.thePrompt=='function')
window.parent.thePrompt('您确定要删除此事件吗?',params);
}
删除

删除

父源

<html>
<head>
<script type="text/javascript">
    function localDel(params){
        if (window.parent && typeof window.parent.thePrompt==='function')
            window.parent.thePrompt('Are you sure you wish to delete this event?', params);
    }
</script>
</head>
<body>
    <p onclick="localDel([1,'some title', 14, 3, 2010, 2])">delete</p>
    <p onclick="localDel([2,'another title', 14, 3, 2010, 2])">delete</p>
</body>
</html>
<html>
<head>
    <script type="text/javascript">

    function thePrompt(msg,params) {
        if(confirm(msg)) theDel.apply(this,params);
    }

    function theDel(id, title, tDay, tMonth, tYear, dDiff) {
      alert(id);
      // etc...
    }

    </script>
</head>
<body>
    <iframe src="iframe.html" />
</body>
</html>

函数提示(消息,参数){
如果(确认(消息))说明适用(此参数);
}
功能标签(id、标题、tDay、tMonth、tYear、dDiff){
警报(id);
//等等。。。
}
我终于拿到了!!耶!:o)

我需要window.frames.theCal来调用我的函数。theCal是我的iframe id


David

正如Jacob指出的那样尝试了这一点。但是我仍然得到了相同的错误。错误:缺失)在参数列表第1行,第68列之后源代码:thePrompt('你确定要删除此项吗?','theDel('105','今天访问我们得50分!!!!','13',3',2010',2'))将其更改为您建议的最后一个会给我带来另一个错误:错误:语法错误行:1,列:9源代码:thePrompt(你的参数在del函数字符串中吗?全部?Drusnov:它们现在作为一个字符串工作,没有提示。但是我想确保用户确定他们想要删除它,因此我需要发送相同的代码将其删除到delete按钮,并像从来没有提示一样执行相同的操作。我尝试过使用它,并且无法将其作为字符串使用..我建议做的一件事是,在delete中实现提示,使其看起来像:if(confirm(“delete User?”){//您甚至可以将其作为提示而不是confirm//在此处执行删除功能}其他{//不执行任何操作}我真的希望这会有帮助,因为我不能让它工作,否则,这真的只是一个建议和一些尝试。这种方式实际上与弹出提示,但删除按钮的值是不正确的…它onclick=“+theDel+;jQuery.prompt.close();”当然,错误在第二个”+“签名。这方面也有错误…错误:语法错误行:1,列:50源代码:thePrompt('您确定要删除此项吗?'),我已删除了换行符,现在应该进行验证。似乎不起作用。它会切断Del中的所有内容。thePrompt”(“你确定要删除这个吗?”,修改了上面的答案,使用了不同的方法。乔纳森,我做了一些事情。请查看我编辑的帖子。@StealthRT:你必须用你想要附加新创建的
img
的元素替换
div
。例如,要附加到你将使用的主体de>document.body.appendChild(img);
。问题在于,xCal.png是弹出提示的按钮,带有“删除”或“取消”按钮,因此我不知道在提示弹出后,在提示内附加什么(或如何)。