English 中文(简体)
如何将“质量要素”选择适用于一个可变变量
原标题:how to apply jQuery element selection to a string variable

我有一位左右的暴徒,然后想利用 j子选择。 这是可能的吗?

例如:

<>光>:

<div class=message>
  This is a message. Click <a class=link id=link1 href=example.com>here</a>
</div>

www.un.org/Depts/DGACM/index_spanish.htm 学历:

$( div.message ).each(function(i, item) {
  myHtml = $(this).html();
  //select <a> tag attributes here
)};

因此,我要从<条码>和<条码>中摘取> 。 Html。

增 编

最佳回答

如果我理解正确的话,你有超文本的软体变数,希望就此提出疑问?

// Suppose you have your HTML in a variable str
var str =  <div class="message">This is a message. Click  
        +  <a class="link" id="link1" href="example.com">here</a></div>​​​​​​​​​​​​​​​ ;

// You query the DOM fragment created by passing the string to jQuery function.
// $(<string>) - creates a DOM fragment (string must contain HTML)
var anchor = $( a , $(str));

// Then you can retrieve individual properties and/or contents:
var id = anchor.attr( id );
var href = anchor.attr( href );
var text = anchor.text();
问题回答

你可以做这样的事情:

var aID = $( div.message a.link ).attr( id );
var aHREF = $( div.message a.link ).attr( href );

有一个 j虫(here),你可以把str作为XML DOM,然后使用j Query对它进行盘问。 它必须是XML,而不是你有时以超文本形式看到的那种“松散的”XML,尽管(即你需要根据你的属性价值援引)。

如果你需要在其结构内处理多个项目,你可以这样做。

$( div.message ).each(function(i, item) { 
  // find desendant of the item using "find"
  var alink = $(this).find( a.link );
  // Process the item
  var id = alink.attr( id ); 
  var href = alink.attr( href ); 
)}; 




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签