Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.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
jQuery替换为JavaScript函数_Javascript - Fatal编程技术网

jQuery替换为JavaScript函数

jQuery替换为JavaScript函数,javascript,Javascript,我有以下代码: $('input.next_page_img').replaceWith('<input type="submit" class="next_page_image graybutton mediumbutton" alt="Next Page" value="Next Page" title="Next Page" onclick="Add_Search_Param('page', 2); Refine();">'); $('input.next_page_img'

我有以下代码:

$('input.next_page_img').replaceWith('<input type="submit" class="next_page_image graybutton mediumbutton" alt="Next Page" value="Next Page" title="Next Page" onclick="Add_Search_Param('page', 2); Refine();">');
$('input.next_page_img')。替换为(“”);
问题在于
onclick
功能。它使脚本的其余部分失败。当我取出
onclick
功能时,它会工作。我需要做什么来解决这个问题


不管怎么说,我最终决定这是个坏主意。相反,我宁愿设置输入的属性。。。为什么这不起作用?

bookmarker=$('').insertBefore('.next_page_img');
$('.next_page_img').detach().attr({type:'submit',value:'save'});

既然您使用的是jQuery,那么在库中添加事件处理程序就不会那么麻烦了:

$('input.next_page_img')
  .replaceWith($('<input type="submit"/>', {
    'class': "next_page_image graybutton mediumbutton",
    'alt': "Next Page",
    'value': "Next Page",
    'title': "Next Page",
    'click': function() {
      Add_Search_Param('page', 2);
      Refine();
    }
  }));
$('input.next\u page\u img')
.替换为($(''){
“类”:“下一页图像灰色按钮mediumbutton”,
“alt”:“下一页”,
“值”:“下一页”,
“标题”:“下一页”,
“单击”:函数(){
添加搜索参数(第2页);
精炼();
}
}));

如果您愿意,您可以在一行中插入所有内容。

为什么不这样做:

$('input.next_page_img').replaceWith($("<input>", {
    type: "submit",
   "class": "next_page_image graybutton mediumbutton",
    alt: "Next Page",
    value: "Next Page",
    title: "Next Page"
}).bind("click", function() {
    Add_Search_Param('page', 2);
    Refine();
}));
$('input.next_page_img')。替换为($(“”){
键入:“提交”,
“类”:“下一页图像灰色按钮中间按钮”,
alt:“下一页”,
值:“下一页”,
标题:“下一页”
}).bind(“单击”,函数(){
添加搜索参数(第2页);
精炼();
}));

您有引号问题。我建议创建元素并用jQuery附加事件处理程序。我最终认为这不是一个好主意。相反,我宁愿设置输入的属性。。。为什么这不管用?我最终决定这是个坏主意。相反,我宁愿设置输入的属性。。。为什么这样不行?我明白。。。我试图在我的网站上实现它,结果是有两个保存按钮。但同时,我也看了剧本,觉得它太“黑”了,不合我的口味。我用一个全文本图像替换src属性并设置按钮的样式,它完全是我想要的,而不是我想要的文本。谢谢你。您的第一个解决方案回答了我最初的问题,因此我将选择您的答案作为正确答案。我最终认为这是一个坏主意。相反,我宁愿设置输入的属性。。。为什么这不起作用?到底是什么让它成为一个“坏主意”?它更容易维护;必须引用带有字符串的原始JavaScript代码是非常难看的。等等,你是说你当初做的是个坏主意?对不起,我现在很困惑:-)@Pointy,“点击”怎么能在那里工作呢。它应该与
$(elem).attr(“click”,function(){})
相同,这没有任何意义。我做错了吗?我的整个想法都不好,因为onclick事件会随着你进入的下一页而改变…@Esailija注意,我将
包装在了它自己的jQuery调用中。当您调用jQuery来创建节点时,可以传递第二个带有要设置的属性的对象参数。库知道在那里查找处理程序名称。它还识别“css”并检查其值是否为对象。
$('input.next_page_img').replaceWith($("<input>", {
    type: "submit",
   "class": "next_page_image graybutton mediumbutton",
    alt: "Next Page",
    value: "Next Page",
    title: "Next Page"
}).bind("click", function() {
    Add_Search_Param('page', 2);
    Refine();
}));