简体   繁体   English

JavaScript代码中的HTML按钮

[英]HTML buttons in javascript codes

I am working in a software which has its interface written in JavaScript I am trying to add an HTML button to the interface by defining a button in the HTML main code, check how I did this http://dpaste.com/691324/ 我正在使用其界面用JavaScript编写的软件,正在尝试通过在HTML主代码中定义按钮来向界面添加HTML按钮,请检查我如何做到这一点http://dpaste.com/691324/

The problem is,, the button appears before the page loads, maybe because the HTML loads before the JS files, I don't know exactly !!! 问题是,该按钮在页面加载之前出现,也许是因为HTML在JS文件之前加载了,我不知道!!! But it really looks ugly, when the button show before the page, and I want to find some trick that can delay the button or to be loaded at the same time with the javascripts..how is this possible? 但是,当按钮在页面之前显示时,它看起来确实很丑陋,我想找到一些可以延迟按钮或与javascript同时加载的技巧。这怎么可能?

I am not a javascript person, but if you are using JQuery, it should go something like this 我不是一个JavaScript的人,但是如果您使用的是JQuery,它应该像这样

$(document).ready(function() {
   document.getElementById('divId').innerHtml = '<input type="button" value="button">';
});

'divId' would be id of Div tag (place holder) covering input tag. “ divId”将是覆盖输入标签的Div标签(占位符)的ID。

Or you can also call some plain javascript function which sets innerHtml of 'divId' on 'Body' tag's onload event, 或者,您也可以调用一些简单的javascript函数,该函数在'Body'标签的onload事件中设置'divId'的innerHtml,

Well I think is that you should use an anchor instead, and if you want, style it as a button. 好吧,我认为您应该改用锚,如果需要,可以将其样式设置为按钮。 Here is the way to create the button with pure JS: 这是使用纯JS创建按钮的方法:

var anchor = document.createElement('a');
anchor.setAttribute('href', '/opentripplanner-tripArabic/index.html');
anchor.setAttribute('class', 'please use CSS'); //inline styling is dirty
anchor.innerHTML = 'use the Arabic interface';
document.getElementById('header').appendChild(anchor);

I recommend to use anchors because you are not using a form, and you only pretend to redirect the user to another page. 我建议使用锚点,因为您没有使用表单,并且只假装将用户重定向到另一个页面。 Either way if you want still the button, you can use document.createElement('button'); 无论哪种方式,如果您仍然想要按钮,都可以使用document.createElement('button'); and asign the property onclick: button.onclick = function(){... instead of the href setting. 并分配属性onclick: button.onclick = function(){...而不是href设置。

Another thing you can do is to hide the button with CSS: display:none and on load wet the element and remove the style: button.style.setProperty('display', ''); 您可以做的另一件事是用CSS隐藏该按钮: display:none并在加载时润湿该元素并删除样式: button.style.setProperty('display', ''); or either way use the CSS propperty visibility: hidden . 或者使用CSS属性visibility: hidden

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

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