简体   繁体   English

Javascript 函数中的 Javascript

[英]Javascript within a Javascript function

I'm obviously not a coder by trade but I'm trying to get this done and can't figureout what I'm missing,我显然不是一个交易员,但我正在努力完成这件事并且无法弄清楚我错过了什么,

This it my current page : http://dev.clearbridgetech.com/case-studies这是我当前的页面: http : //dev.clearbridgetech.com/case-studies

Currently if I click on the "By Practice" button the text "Practice......." is outputed to the bottom as expected but instead id like the code below labeled as "Blue JavaScript" below and I just can't figure out how to do this.目前,如果我点击“By Practice”按钮,文本“Practice.......”会按预期输出到底部,但 id 就像下面标记为“Blue JavaScript”的代码一样,我就是不能弄清楚如何做到这一点。 Any help will be greatly appreciated.任何帮助将不胜感激。

<script language="JavaScript">
function bypractice()
{
document.getElementById("divA").innerHTML="Practice.......";
}
</script></p>
<script language="JavaScript">
function byverticle()
{
document.getElementById("divA").innerHTML="Verticle.......";
}

Blue JavaScript蓝色 JavaScript

<p style="text-align: center;">
<a href="javascript:appdev();"><img alt="" src="images/cs-images/appdev.png" style="width: 100px; border-width: 0px; border-style: solid; height: 31px;" /></a>&nbsp; <a href="javascript:cloudcomp();"><img alt="" src="images/cs-images/cloudcomp.png" style="width: 100px; height: 31px; border-width: 0px; border-style: solid;" /></a>&nbsp; <a href="javascript:dbsol();"><img alt="" src="images/cs-images/dbsol.png" style="width: 100px; border-width: 0px; border-style: solid; height: 31px;" /></a>&nbsp; <a href="javascript:dcinfra();"><img alt="" src="images/cs-images/datainfra1.png" style="width: 100px; border-width: 0px; border-style: solid; height: 31px;" /></a></p>

Replace "Practice....." with "< your Blue Javascript HTML here >"用“< your Blue Javascript HTML here >”替换“Practice.....”

Your HTML code should be inside quotation marks, just as if it were a string.您的 HTML 代码应该在引号内,就像它是一个字符串一样。 ie document.getElementById('myDiv').innerHTML = "<b style='color:#f00'>This will print red bolded text</b>";document.getElementById('myDiv').innerHTML = "<b style='color:#f00'>This will print red bolded text</b>";

You'll need to use single quotes instead of double quotes in the HTML, or escape your double quotes by adding the '\\' character prior to each quotation mark.您需要在 HTML 中使用单引号而不是双引号,或者通过在每个引号之前添加 '\\' 字符来转义双引号。

i'm not sure to understand but if what you want is to show different html block (with text, js, images inside it) depending on a clicked button i think you'll find an easier way putting all the different blocks in the page, show the first and hide the others.我不确定是否理解,但是如果您想要根据单击的按钮显示不同的 html 块(其中包含文本、js、图像),我想您会找到一种更简单的方法将所有不同的块放在页面中,显示第一个并隐藏其他。 Then when the user click the buttons you hide the unwanted and vice versa.然后,当用户单击按钮时,您会隐藏不需要的按钮,反之亦然。

You could:你可以:

  • define a css class .DisplayNone {display: none;}定义一个 css 类.DisplayNone {display: none;}
  • assign a name to the different blocks to show (divA, divB, divC) setting class="DisplayNone" to all but the first为不同的块分配一个名称以显示 (divA, divB, divC) 设置class="DisplayNone"到除第一个以外的所有块
  • when the user click on a button you assign the DisplayNone class to all the divA, divB, divC blocks with document.getElementById("divA").className="DisplayNone";当用户单击按钮时,您将 DisplayNone 类分配给所有具有document.getElementById("divA").className="DisplayNone"; divA document.getElementById("divA").className="DisplayNone"; divA、divB、divC 块document.getElementById("divA").className="DisplayNone"; and in the end remove it from the block you want to show document.getElementById("divC").className="";最后将其从要显示的块中删除document.getElementById("divC").className="";
  • using jQuery instead of pure javascript might make your code simpler使用 jQuery 而不是纯 javascript 可能会使您的代码更简单

How about putting all the HTML that you need in a div and applying the style display:none and then on click of button replacing the innerHTML of you div with innerHTML of the hidden div.如何将您需要的所有 HTML 放在一个 div 中并应用样式display:none然后单击按钮将您的 div 的 innerHTML 替换为隐藏 div 的 innerHTML。

Something like this.像这样的东西。

function bypractice()
{
document.getElementById("divA").innerHTML=document.getElementById("hiddendiv").innerHTML;
}

DEMO演示

My good friend was actually able to figure this out rather quickly.我的好朋友实际上能够很快地弄清楚这一点。 "Basically, you're wrapping the tag with the tag, so that the image becomes a link, and, in turn, invokes the JS." “基本上,你是用标签包装标签,这样图像就变成了一个链接,进而调用 JS。”

<a href="javascript:appdev();"><img alt="" src="/images/cs-images/appdev.png" style="width: 100px; border-width: 0px; border-style: solid; height: 31px;" /></a>

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

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