简体   繁体   English

javascript函数 - 正确的方法

[英]javascript function - the correct way to do this

Hi all and thanks for looking, 大家好,感谢您的光临,

What is the correct way to do this: 这样做的正确方法是什么:

<script language="javascript">
function flag(nation)
{
this.nation=nation;
document.getElementById("flag").innerHTML="<img src='images/flags/'+nation+'.jpg'>";
}
</script>

with this in the link tags: onClick="flag(scotislands)"; 在链接标签中有这个:onClick =“flag(scotislands)”; and the image name being scotislands.jpg 而图像名称是scotislands.jpg

Many thanks, B. 非常感谢,B。

In the following line you have your string identifiers mixed up: 在以下行中,您将字符串标识符混淆:

document.getElementById("flag").innerHTML="<img src='images/flags/'+nation+'.jpg'>"; 

It should be: 它应该是:

document.getElementById("flag").innerHTML='<img src="images/flags/'+nation+'.jpg">'; 

EDIT 编辑
Also, as Joel spotted, onClick="flag(scotislands)"; 此外,正如乔尔所发现的, onClick="flag(scotislands)"; should be onClick="flag('scotislands')"; 应该是onClick="flag('scotislands')";

You need to quote the text in the link tag, otherwise it tries and fails to find a variable with the name scotislands . 您需要引用链接标记中的文本,否则它会尝试并且无法找到名为scotislands的变量。

 onClick="flag('scotislands');"

then change the assignment of the innerHTML to the following: 然后将innerHTML的赋值更改为以下内容:

 document.getElementById("flag").innerHTML="<img src='images/flags/" + nation + ".jpg'>"

Note the use of single quotes internally to set of the url, but double quotes around each of the inline text bits. 请注意在内部使用单引号来设置url,但在每个内联文本位周围使用双引号。

Generally, though, I'd prefer something unobtrusive (no code in markup). 但一般来说,我更喜欢不引人注目的东西(标记中没有代码)。 Using a framework, such as jQuery, I'd do something like: 使用jQuery之类的框架,我会做类似的事情:

 <a href="#scotislands" class="flag-identifier">Scotislands</a>

Then using javascript, I'd add a handler for all such links: 然后使用javascript,我将为所有这些链接添加一个处理程序:

 $(function() {
     $('a.flag-identifier').click( function() {
         var flag = $(this).attr('href').replace('#','');
         $('#flag').html( '<img src="images/flags/' + flag + '.jpg" />' );
         return false;
     });
 });

This would add a click handler that gets the flag name from the href on the anchor, then replaces the named element flag 's content with the image constructed by adding the name of the country to the image url. 这将添加一个单击处理程序,该处理程序从锚点上的href获取标志名称,然后将指定元素flag的内容替换为通过将国家/地区名称添加到图像URL构建的图像。 Note that I omitted the global variable nation , but you could easily set it from within the click handler as well if necessary. 请注意,我省略了全局变量nation ,但如果需要,您也可以在click处理程序中轻松设置它。

The string that you are setting innerHTML to is not quoted properly. 您正在设置innerHTML的字符串未正确引用。 Try this: 尝试这个:

document.getElementById("flag").innerHTML="<img src='images/flags/'+nation+'.jpg'>";

Also, your onClick is trying to send an non-existent object called "scotislands". 此外,您的onClick正在尝试发送一个名为“scotislands”的不存在的对象。 Try this instead: 试试这个:

onClick="flag('scotislands');"
function flag(nation) {
  var myFlag = document.getElementById("flag").getElementsByTagName("img")[0];
  myFlag.src = "images/flags/"+nation+".jpg";
}

It depends what you mean by "correct". 这取决于你所说的“正确”。 I suspect from the fact you're using innerHTML that you don't mean "conforming to a recognised standard, such as DOM", but that's what you're getting from me: 我怀疑你使用的是innerHTML ,你并不是说“符合公认的标准,比如DOM”,但这就是你从我这里得到的:

function flag(nation) {
    var flagEl = document.getElementById("flag");
    while (flagEl.firstChild) {
        flagEl.removeChild(flagEl.firstChild);
    }
    var img = document.createElement("img");
    img.src = "images/flags/" + nation + ".jpg";
    flagEl.appendChild(img);
}

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

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