需要帮助将appendChild更改为replaceChild

时间:2017-08-20 21:53:11

标签: javascript html dom appendchild

有一种简单的方法可以将其从appendChild更改为replaceChild吗?

这当然会不断增加更多元素。另外由于某种原因,它没有将值放在DIVSPAN内,似乎放在它下面。

var para = document.createElement("P");
var total = document.createTextNode(parseFloat((subT + tax).toFixed(2))) ;

para.appendChild(total);

document.getElementById("total_id").appendChild(para);

更新此内容:

<div class="prod_totals">Order Total: $<span id="total_id"></span></div>

2 个答案:

答案 0 :(得分:0)

您只需使用innerHTML代替appendChild

即可
document.getElementById("total_id").innerHTML = parseFloat((subT + tax).toFixed(2));

因为您没有在total_id元素中插入任何用户输入值,并且就问题提及而言,其数据不会传递到服务器我认为您使用{{{ 1}}这里。但是,如果出于任何原因你仍然想使用innerHTML,你可以这样做:

replaceChild

答案 1 :(得分:0)

此处无需使用.replaceChild,您只需在尝试更新之前检查package com.example.lucam.helloworld; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.view.View; import android.widget.EditText; import android.widget.TextView; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Arrays; import com.google.api.services.translate.Translate; import com.google.api.services.translate.model.TranslationsListResponse; import com.google.api.services.translate.model.TranslationsResource; import translate.*; public class MainActivity extends AppCompatActivity { private Button bottone1; private EditText testoInput; private TextView testoOutput; private Translate translator; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bottone1= (Button) findViewById(R.id.button); testoInput= (EditText) findViewById(R.id.editText); testoOutput= (TextView) findViewById(R.id.textView); bottone1.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ Translate t = null; try { t = new Translate.Builder( com.google.api.client.extensions.android.http.AndroidHttp.newCompatibleTransport() , com.google.api.client.extensions.android.json.AndroidJsonFactory.getDefaultInstance(), null) //Need to update this to your App-Name .setApplicationName("helloworld") .build(); } catch (GeneralSecurityException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Translate.Translations.List list = null; try { list = t.new Translations().list( Arrays.asList( //Pass in list of strings to be translated "Hello World", "How to use Google Translate from Java"), //Target language "ES"); } catch (IOException e) { e.printStackTrace(); } //Set your API-Key from https://console.developers.google.com/ list.setKey("my api code"); TranslationsListResponse response = null; try { response = list.execute(); } catch (IOException e) { e.printStackTrace(); } TranslationsResource tr = response.getTranslations(); testoOutput.setText(tr.getTranslatedText()); } }); } } 是否已创建。

请注意,您试图在element中插入p元素,这是错误的,是无效的HTML标记,您可以在the span documentation中看到其可能的内容仅为Phrasing content,因此您最好使用其他span

这应该是你的代码:

span

这是一个演示:

var para = document.querySelector("#total_id span");
if (!para || para.length == 0) {
  para = document.createElement("span");
}
var total = parseFloat((subT + tax).toFixed(2));
para.innerText = total;
document.getElementById("total_id").appendChild(para);
var para = document.querySelector("#total_id span");
if (!para || para.length == 0) {
  para = document.createElement("span");
}
var total = new Date();

para.innerText = total;

document.getElementById("total_id").appendChild(para);