问题答案 12026年5月28日 13:47
WebView jump to anchor using loadDataWithBaseUrl
When loading HTML content in WebView, we may need to directly navigate to a specific anchor point on the page. The method allows setting a base URL while loading data, which is particularly well-suited for handling such cases.Steps OverviewDefine HTML Content: First, define your HTML content and ensure it includes anchor points.Use the Method: Load the HTML content using this method and set the appropriate base URL.Navigate to Anchor Point: After the HTML content is loaded, use JavaScript or an appropriate method to navigate to the specific anchor point.ExampleSuppose you have the following HTML content:You want to load this HTML in WebView and directly navigate to the section with ID .Java Code ExampleNotesBase URL Setting: Ensure that the correctly points to the base path of your HTML content, which is critical for loading resources like images and CSS.JavaScript Support: Since JavaScript is used to navigate to the anchor point, ensure that JavaScript is enabled in WebView.Post-Load Navigation: Ensure that the JavaScript code for navigating to the anchor point is called after the page has fully loaded. This can be achieved using the method of .By using this approach, we not only load the HTML content but also achieve direct navigation to anchor points within the page using .