我怎样才能从链接获取名称? [英] How can I get name from link?

查看:98
本文介绍了我怎样才能从链接获取名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写Objective-C。
我有 WebView 和本地文件index.html有

 < a href ='http://www.google.com'name =666> 

如何获取名称属性?



感谢!

解决方案

需要获取名称。如果某人点击该链接时需要该名称,则可以设置一些在单击该链接时运行的JavaScript(onclick handler)。如果您只有html字符串,则可以使用正则表达式来解析文档并提取所有名称属性。 Objective-C的一个好的正则表达式库是 RegexKit (或RegexKitLite在同一页上) 。

从链接中解析name属性的正则表达式看起来像这样:

  /< a [^>] +?name =?([^>] *)?> / i 

编辑:有人点击链接时从链接中获取名称的JavaScript看起来像这样:


$ b $

  function getNameAttribute(element){
alert(element.name); //或者用名称`element来做别的事情。 name`包含name属性的值。
}

这会从 onclick 处理程序,如下所示:

 < a href =http ://www.google.com/name =anElementNameonclick =getNameAttribute(this)>我的链接< / a> 

如果您需要将名称恢复为您的Objective-C代码,您可以编写onclick函数,以hashtag形式将name属性附加到url,然后捕获请求并将其解析到您的UIWebView中委托的 -webView:shouldStartLoadWithRequest:navigationType:方法。这将会是这样的:

pre $函数getNameAttribute(元素){
element.href + ='#'+ element.name;
}

//然后在你的委托的.m文件中
$ b - (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest * )请求
navigationType:(UIWebViewNavigationType)navigationType {

NSArray * urlParts = [[请求URL] componentsSeparatedByString:@#];
NSString * url = [urlParts objectAtIndex:0];
NSString * name = [urlParts lastObject];
if([url isEqualToString:@http://www.google.com/]){
//用`name`做些事
}

返回FALSE; //或者如果你想要关注链接
}


I'm writing on objective-C. I have WebView and local file index.html has

<a href='http://www.google.com' name="666">

How can I get the name attribute?

Thanks!

解决方案

It depends on when/by what you need to get the name. If you need the name when someone clicks on the link, you can set up some JavaScript that runs when the link is clicked (onclick handler). If you just have the html string, you can use regular expressions to parse the document and pull out all of the name attributes. A good regular expression library for Objective-C is RegexKit (or RegexKitLite on the same page).

The regular expression for parsing the name attribute out of a link would look something like this:

/<a[^>]+?name="?([^" >]*)"?>/i

EDIT: The javascript for getting a name out of a link when someone clicked it would look something like this:

function getNameAttribute(element) {
    alert(element.name); //Or do something else with the name, `element.name` contains the value of the name attribute.
}

This would be called from the onclick handler, something like:

<a href="http://www.google.com/" name="anElementName" onclick="getNameAttribute(this)">My Link</a>

If you need to get the name back to your Objective-C code, you could write your onclick function to append the name attribute to the url in the form of a hashtag, and then trap the request and parse it in your UIWebView delegate's -webView:shouldStartLoadWithRequest:navigationType: method. That would go something like this:

function getNameAttribute(element) {
    element.href += '#'+element.name;
}

//Then in your delegate's .m file

- (BOOL)webView:(UIWebView *)webView
        shouldStartLoadWithRequest:(NSURLRequest *)request
        navigationType:(UIWebViewNavigationType)navigationType {

    NSArray *urlParts = [[request URL] componentsSeparatedByString:@"#"];
    NSString *url = [urlParts objectAtIndex:0];
    NSString *name = [urlParts lastObject];
    if([url isEqualToString:@"http://www.google.com/"]){
        //Do something with `name`
    }

    return FALSE; //Or TRUE if you want to follow the link
}

这篇关于我怎样才能从链接获取名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆