我该如何调用该功能? [英] How do I call the function?
问题描述
< !DOCTYPE html >
< html >
< head >
< div 类 = block 样式 = padding-removed 60px; >
< 正文 >
< center >
< div class = vertical >
< span class =code-keyword>< img src = google.png / >
< / div >
< div class = mid style < span class =code-keyword> = padding-removed 5px; >
< / div >
< div class = block style = padding-removed 0px; >
< 表格 action = search.cgi >
< 输入 style = width:620px; height: 32PX;>
< input width:500px; height:20px; < span class =code-attribute> type = text 名称 = 搜索 id = tipue_search_input autocomplete = off 必需 >
< / form >
&l t; script LANGUAGE = JavaScript >
功能搜索功能(){
var searching2 = document .getElementByName( search)。value;
if (searching2 === Reuben ){
< a href = godpg1.html> ;
} else if (searching2 === Brayden){
< a href = retardpg1.html跨度>>
} else if (searching2 === scrub){
< a href = retardpg1.html跨度>>
} else if (searching2 === brayden){
< a href = retardpg1.html跨度>>
} else if (searching2 === 延迟){
< a href = retardpg1.html跨度>>
} else if (searching2 === 延迟){
< a href = retardpg1.html跨度>>
} else if (searching2 === reuben){
< a href = godpg1.html跨度>>
} else if (searching2 === god){
< a href = godpg1.html跨度>>
} else if (searching2 === 上帝){
< a href = godpg1.html跨度>>
} else if (searching2 === legend){
< a href = godpg1.html跨度>>
} 其他 {
< a href = == others.html >
}
< / script >
< div class = block 样式 = padding-removed 30px; >
< img < span class =code-attribute> src = google Im feeling lucky.png alt = 谷歌我感到很幸运 style = width: 250px ;高度:36px; >
< / div > < / a > < / a > < / a > < / a > < / A > < / a > < / a > < / a > < / a > < / a > < / a > < / div > < / center >
< / body >
< / html > < / div >
您无法以任何方式调用它甚至使用代码。它应该是功能,而不是功能。如果没有修复拼写错误,即使其他所有内容都正确并且执行从未达到这一点,您也无法使用此代码甚至在此错误定义之前。对脚本进行词法分析会使其失败;即使你把所有东西放在try-catch块下面,你也无法从这个问题中恢复过来。
假设你说,你修复它。怎么称呼它?奇怪的问题。这里:
searchfunction();
顺便说一下,像* function这样的单词用作函数的名称是荒谬的。我永远无法理解人们如何提到发明包含单词application或app的应用程序的名称以及包含单词class的类的名称。当然,这取决于你,但它的外观(叹息......)。
我回答了你的问题,因为你只问如何调用它。但它不起作用,因为功能本身是错误的。你没有问如何修复这个功能,但我已经告诉你一个修复。另一个问题是:您不能将HTML代码与JavaScript代码混合使用。您可以执行以下操作:在您的HTML中创建一个静态< a href = ...>
,添加和< code> id属性并使用它找到此锚点document.getElementById
以同样的方式。然后,根据某些计算,为其指定href
所需的值。这是由元素的setAttribute
函数完成的: Element.setAttribute() - Web API | MDN [ ^ ]。
这就是全部,但你需要学习更多东西。即使你解决了所有问题,也不可能以如此肮脏的方式编写代码。首先,所有这些立即常量,例如scrub,braiden等等,对于维护来说都是完全不好的。代码应该是数据驱动的。创建一个描述数据的单独对象。如果这些单词是产品名称,您可以创建一个产品名称数组,一些对象,比如由产品名称或产品实例索引的URL字典,类似的东西。您需要以易于维护,易读的方式编写代码。你当然需要精益求精,但也需要通用编程。你可以从这里开始:
JavaScript - 维基百科,免费的百科全书 [ ^ ],
HTML - 维基百科,免费的百科全书 [ ^ ],等等...
第二个想法,我不确定你需要那些不同的href
。目前尚不清楚你是否希望所有这些都在一个页面上,或者这个页面应该根据输入显示不同的URL ...也许你只需要一个元素导航到某个页面,静态编写,但是点击
事件处理程序可以读取您的输入本身(同样的事情,document.getElementById
,依此类推)并根据数据计算URL。导航的几种方法之一是使用Window.location
对象:
Window.location - Web API | MDN [ ^ ]。
最后,还有一个更具吸引力的可能性。如何不在当前页面的任何位置导航?据我了解,您必须显示一些产品属性,具体取决于用户选择的产品类型。为什么不在同一页面上动态显示所有属性?这并不难。您只需要在页面的一部分上修改HTML DOM。
让我们考虑一种最简单的方法。比方说,例如,你有一些div
(带有id
来获取对所有DOM节点的引用,就像所有上面讨论的例子)。根据某些选定的输入数据,将整个HTML内容计算为HTML字符串(例如,newHTMLContent
)。单击某个按钮(或者不是,例如,列表框事件),您可以计算特定于所选产品/选项/等的此字符串,并分配myDiv.innerHTML = newHTMLContent;
在更彻底和更少自由形式的方法中,您可以预定义HTML并仅更改其中的一些元素。
-SA
<!DOCTYPE html>
<html>
<head>
<div class="block" style="padding-removed 60px;">
<body>
<center>
<div class="vertical">
<img src="google.png"/>
</div>
<div class="mid" style="padding-removed 5px;">
</div>
<div class="block" style="padding-removed 0px;">
<form action="search.cgi">
<input style="width:620px;height:32px;>
<input "width:500px;height:20px;" type="text" name="search" id="tipue_search_input" autocomplete="off" required>
</form>
<script LANGUAGE="JavaScript">
funtion searchfunction() {
var searching2 = document.getElementByName ("search").value;
if (searching2 === "Reuben") {
<a href="godpg1.html">
} else if (searching2==="Brayden") {
<a href="retardpg1.html">
} else if (searching2==="scrub") {
<a href="retardpg1.html">
} else if (searching2==="brayden") {
<a href="retardpg1.html">
} else if (searching2==="retard") {
<a href="retardpg1.html">
} else if (searching2==="Retard") {
<a href="retardpg1.html">
} else if (searching2==="reuben") {
<a href="godpg1.html">
} else if (searching2==="god") {
<a href="godpg1.html">
} else if (searching2==="God") {
<a href="godpg1.html">
} else if (searching2==="legend") {
<a href="godpg1.html">
} else {
<a href="=="others.html"">
}
</script>
<div class="block" style="padding-removed 30px;">
<img src="google Im feeling lucky.png" alt="google Im feeling lucky" style=width:"250px;height:36px;">
</div></a></a></a></a></a></a></a></a></a></a></a></div></center>
</body>
</html></div>
You cannot call it or even use the code in any way. It should be "function", not "funtion". Without fixing typo, you won't be able to use this code even coming before this incorrect definition, even if everything else is correct and execution never reach this point. Lexical parsing of the script will fail it; you won't recover from this problem even if you put everything under the try-catch block.
Let's say, you fix it. How to call it? Strange question. Here:
searchfunction();
By the way, a word like "*function" used as a name of a function is absurd. I never can understand how people mention to "invent" the name of application containing the word "application" or "app" and the name of a class containing the word "class". It's up to you, of course, but the way it looks (sigh…).
I answered your question, because you only asked how to call it. But it won't work, because the function itself is wrong. You did not ask how to fix the function, but I already told you one fix. Another problem is: you cannot mix HTML code with JavaScript code. Here is what you can do: in your HTML create a static<a href=… >
, add and <code>id attribute to it and find this anchor usingdocument.getElementById
in the same way. Then, depending on some calculations, assign required value ofhref
to it. This is done by the element'ssetAttribute
function: Element.setAttribute() - Web APIs | MDN[^].
That's all, but you need to learn a lot more. Even if you fix it all, it's impossible to write code in such a dirty way. First of all, all these immediate constants, such as "scrub", "braiden", and so on, are utterly bad for maintenance. Code should be data-driven. Create a separate object describing you data. If these words are product names, you can create an array of product names, some objects, say, a dictionary of URLs indexed by product name or product instances, something like that. You need to write code in easily maintainable, well readable way. And you certainly need to lean JavaScript, but also general programming. You can start here:
JavaScript - Wikipedia, the free encyclopedia[^],
HTML - Wikipedia, the free encyclopedia[^], and so on…
[EDIT]
On second thought, I'm not really sure you need those differenthref
. It's not clear if you want all of them on one page, or this page should expose different URLs depending on input… Perhaps you need just one element to navigate to some page, statically written, butclick
event handler could read your input itself (same thing,document.getElementById
, and so on) and calculate the URL based on the data. One of the few ways to navigate is using theWindow.location
object:
Window.location — Web APIs | MDN[^].
And, finally, there is an even more attractive possibility. How about not navigating anywhere from the present page at all? As I understand, you have to show show some product properties, depending on the product type chosen by the user. Why not showing all properties dynamically on the same page? This is not hard to do. You just to need to modify the HTML DOM on part of your page.
Let's consider one most straightforward way to do so. Let's say, for example, you have somediv
(withid
to get the reference to its DOM node as in all examples discussed above). Depending on some chosen input data, calculate the whole HTML content as an HTML string (say,newHTMLContent
). On click on some button (or no, say, list box event), you calculate this string specific to chosen product/options/etc., and assignmyDiv.innerHTML = newHTMLContent;
in more thorough and less free-form approach, you can have predefined HTML and change only some elements in it.
—SA
这篇关于我该如何调用该功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!