简体   繁体   English

将innerhtml javascript转换为jquery

[英]converting innerhtml javascript to jquery

I'm learning jQuery so I've been stepping through my old code trying to change everything over and so far its been pretty easy until I ran into this. 我正在学习jQuery,所以我一直在逐步浏览旧代码,尝试更改所有内容,到目前为止,这很容易,直到遇到此问题为止。

I have this bit of javascript code 我有这段JavaScript代码

myText.innerHTML += "<li>"+input_text+" "+input_date+" "+input_time+"</li>";}

working with this bit of html code 使用这段HTML代码

<button onClick='buttonClicked'>Submit!</button>

which I assumed I would convert to 我以为我会转换成

<input type ="button" id ='buttonClicked' value="Submit!"/>

I just haven't seemed to get the jQuery right for this quite yet and its getting a little frustrating. 我似乎还没有完全正确地使用jQuery,并且它有点令人沮丧。

Obviously there's more to it than just the code I gave, I'm just trying to keep everyone focused, if you really need more just ask, I've already converted the rest of the js to jQuery though. 显然,除了我提供的代码之外,还有更多其他功能,我只是想让所有人都专注于它,如果您真的需要更多,请问一下,我已经将其余的js转换为jQuery。 My friend said I should be using 'append' but, so far I haven't been able to figure it out. 我的朋友说我应该使用“附加”,但到目前为止,我还没有弄清楚。

Can you convert this and link me to some documentation as to why it works? 您可以将其转换为我,然后将其链接至一些文档以了解其工作原理吗? Let me know it would be tremendously helpful for bringing my comprehension of this stuff a lot quicker, thank you! 让我知道,这对加快我对这些东西的理解会非常有帮助,谢谢!

edit: Was 编辑:是

function buttonClicked() {
var myText = document.getElementById('myText');
var input_text = document.getElementById('input_text').value  ; 
var input_date = document.getElementById('input_date').value  ;        
var  input_time = document.getElementById('input_time').value  ; 
myText.innerHTML += "<li>"+input_text+" "+input_date+" "+input_time+"</li>";}

so far in jQuery is: 到目前为止,jQuery是:

$(document).ready(function() {
var input_text = $('#input_text').val();   
var input_date = $('#input_date').val();
var input_time = $('#input_time').val();
$('#buttonClicked').click(function(){
  //$('#myText').append(function(){ $("<li> "+input_text+" "+input_date+" "+input_time+" </li>") 
//$('myText').html();

});


});

I've tried a lot but I'm not sure where to start. 我已经尝试了很多,但不确定从哪里开始。

Assuming you have something like this on your HTML: 假设您在HTML上有类似的内容:

<input type="button" id="buttonClicked" value="Submit!">
<div id="myText></div>

You can use jQuery like this: 您可以像这样使用jQuery:

$('#buttonClicked').click(function () {
    $('#myText').append("<li>"+input_text+" "+input_date+" "+input_time+"</li>");
});

The first step is adding a click handler to your button with JQuery. 第一步是使用JQuery将单击处理程序添加到您的按钮。 Docs. 文件。

$("#buttonClicked").click(function() {
  //handle click
});

You'll want to use the append method in that function that's being called by the click handler. 您需要在点击处理程序正在调用的函数中使用append方法。 The docs for that can be found here . 可以在此处找到该文档。

Add a click handler to your button and use $.append to insert the content that you want. 将单击处理程序添加到您的按钮,然后使用$.append插入所需的内容。

You can see the documentation here: http://api.jquery.com/append/ 您可以在此处查看文档: http : //api.jquery.com/append/

$(function(){
   $('#buttonClicked').click(function(){
      var input_text = $('#input_text').val();
      var input_date = $('#input_date').val();
      var input_time = $('#input_time').val();
      $('#myText').append("<li>"+input_text+" "+input_date+" "+input_time+"</li>");
   });

}); });

The other answers are valid, but I suggest using "on", for the click event: 其他答案是有效的,但我建议对点击事件使用“ on”:

$("#buttonClicked").on("click", function(e){
    e.preventDefault(); //this, along with return false, will prevent default behavior from button clicks - probably only useful in web applications, but a good convention to start using
    $('#myText').append("<li>"+input_text+" "+input_date+" "+input_time+"</li>"); // Note that this will continue to append additional li elements with each button click.  If that is not desired, you can call $("#myText").empty(); before adding the li element
    return false;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM