简体   繁体   English

如何让我的 js 代码 select 成为正确的 html 页面供选择?

[英]how can i make my js code select the right html page to choose?

So I have a search engine for my country's states ( made with html, css and js) and for each state I want to make an html page, so when I go to the search bar and type the state's name and I click the search icon it shows the html page that belongs to that stat (there are 48 html page)所以我有一个针对我国各州的搜索引擎(使用 html、css 和 js 制作),对于每个 state 我想制作一个 html 页面,所以当我 go 到搜索栏并输入州名时,我单击搜索图标它显示属于该统计的 html 页面(有 48 个 html 页面)

function select(element)
{
    let selectData = element.textContent;
    inputBox.value = selectData;
    icon.onclick = ()=>
    {
        webLink = `https://www.google.com/search?q=${selectData}`;
        linkTag.setAttribute("href", webLink);
        linkTag.click();
    }
}

I tried this way, but it make a research on the state in google, I don't want that.我试过这种方式,但它在谷歌中对 state 进行了研究,我不想要那样。 I want to show my html page我想显示我的 html 页面

I also tried this:我也试过这个:

webLink = htmls/?q=${selectData} ; webLink = htmls/?q=${selectData} ; linkTag.setAttribute("href", webLink); linkTag.setAttribute("href", webLink); linkTag.click(); linkTag.click();

i removed the google link and ive out the name of the folder in which the html pages are stored now when i click enter an index page comes out and i have to choose which html page to show manually, how can i make it choose automatically?我删除了 google 链接并找出了存储 html 页面的文件夹的名称,现在当我单击输入时会出现一个索引页面,我必须选择手动显示哪个 html 页面,我如何让它自动选择?

Instead of a linkTag.click().click() , how about using location.href = webLink .而不是linkTag.click().click() ,如何使用location.href = webLink This way, it'll directly replace the browser url and will input exactly the value of webLink.这样,它会直接替换浏览器 url 并准确输入 webLink 的值。 Also, it won't propagate events.此外,它不会传播事件。

There are several ways to make your JavaScript code select the right HTML page to choose, depending on your specific needs and requirements.有几种方法可以使您的 JavaScript 代码 select 正确的 HTML 页面可供选择,具体取决于您的需要和要求。 Some common methods include:一些常见的方法包括:

Using the DOM API: The Document Object Model (DOM) API allows you to interact with the HTML and XML documents, you can use this API to select the specific elements you want to target and perform operations on them. Using the DOM API: The Document Object Model (DOM) API allows you to interact with the HTML and XML documents, you can use this API to select the specific elements you want to target and perform operations on them. You can use the DOM API to select elements by their tag name, class name, or ID, and then manipulate them using JavaScript.您可以通过标记名称、class 名称或 ID 使用 DOM API 到 select 元素,然后使用 JavaScript 操作它们。

Using jQuery: jQuery is a popular JavaScript library that makes it easy to select and manipulate elements on a web page.使用 jQuery:jQuery 是一个流行的 JavaScript 库,可以轻松地 select 和操作 web 页面上的元素。 You can use jQuery's selectors to select elements by their tag name, class name, or ID, and then perform operations on them using jQuery methods.您可以使用 select 元素的标签名称、class 名称或 ID,使用 jQuery 的选择器,然后使用 jQuery 方法对它们执行操作。

Using a JavaScript framework: JavaScript frameworks such as React, Angular, or Vue.js, provide a structure to your code, and also provide a set of tools to help you manage the state and rendering of your application.使用 JavaScript 框架:JavaScript 框架,例如 React、Angular 或 Vue.js,为您的代码提供结构,还提供一组工具来帮助您管理 state 和应用程序的渲染。 You can use these frameworks to create a single-page application (SPA) and use routing to select the right HTML page.您可以使用这些框架创建单页应用程序 (SPA) 并使用路由到 select 正确的 HTML 页面。

Using a server-side language: If you are using a server-side language such as Node.js, you can use it to handle the routing and serve the right HTML page based on the user's request.使用服务器端语言:如果您使用服务器端语言,例如 Node.js,您可以使用它来处理路由,并根据用户的请求提供正确的 HTML 页面。 This will allow you to handle the routing logic on the server, and leave the JavaScript code to handle only the client-side logic.这将允许您处理服务器上的路由逻辑,并让 JavaScript 代码仅处理客户端逻辑。

Using a routing library: There are several routing libraries available for JavaScript such as React Router or Vue Router, that you can use to handle the routing in your application.使用路由库:JavaScript 有几个可用的路由库,例如 React Router 或 Vue Router,您可以使用它们来处理应用程序中的路由。 These libraries provide a set of tools to handle client-side routing and allow you to map URLs to specific components in your application.这些库提供了一组工具来处理客户端路由,并允许您将 map URL 指向应用程序中的特定组件。

It's worth noting that the best solution will depend on the specific requirements of your project, the complexity of your codebase, and the technologies you are using.值得注意的是,最佳解决方案将取决于项目的具体要求、代码库的复杂性以及您使用的技术。

暂无
暂无

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

相关问题 如何在 js 中循环遍历我的 json 数据以在我的 html 页面中制作卡片 - How can I loop through my json data in js to make cards in my html page 我怎样才能让我的 Javascript 代码 select 在页面上显示所有“b”标签,而不是只有第一个? - How can I make my Javascript code select all 'b' tags on a page instead of only the first one? p5.j​​s:如何根据HTML页面使草图加载不同的.txt文件? - p5.js: How can I make my sketch load a different .txt file depending on the HTML page? 如何使用CSS和JS编写的多选代码仅选择一个选项? 目前,我可以同时选择 - How do I make my multiple choice code written in CSS & JS to only select one option? Currently I can select both 有人能告诉我,我对HTML / JS代码的解释是否正确? - Can someone tell me if I am right in my explanation of HTML/JS code? 如何在源代码中隐藏部分 html/js? - How can I hide part of html/js in my source code? 如何在HTML和PHP中实现此JS代码? - How can I implement this JS code in my HTML and PHP? 我如何制作一个使用特定输入重定向到特定 html 页面的 js 代码? - How do i make a js code that redirects to a certain html page with a certain input? 如何将+按钮移到网页右侧? - How can I move my + button to the right of my web page? 如何在 HTML 页面上打印带有颜色语法高亮显示的 HTML 代码? - How can I print HTML code with colors syntax highlighting on my HTML page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM