0

JqueryまたはJavascriptのいずれかでHTMLファイルから次の3つの値を選択したいと思います。

  1. クラス"class1"href value
  2. class "class1"内部テキスト値(サンプルコードのPersonA)
  3. クラス「タイトル」の内部テキスト値(例では会計士)

liのすべてのデータをノードごとに選択するにはどうすればよいですか?迷っています :(

<ol id="result-set">
<li id="v-0">
    <div class="result-data">
    ..
    <h2>
        <a class="class1" href="">PersonA</a>
    </h2>
    <dl class="basic">
        <dt>Title</dt>
        <dd class="title">Accountant</dd>
        ....
    </dl>
    </div>
</li>
<li id="v-1">
...
</li>
..... 
4

4 に答える 4

5

With jQuery, you can do something like this:

$("#result-set li").each(function() {
   var $currentLi = $(this),
       $class1link = $currentLi.find("a.class1"),
       class1href = $classAlink.attr("href"),
       class1content = $classAlink.html();

   // do something with values
});

The .each() method will process each li element. Within the callback to .each() the variable $currentLi is a jQuery object holding that li (set from $(this) where this is the li element itself). The .find() method is used to find the anchor element within the li and then its href and content are retrieved.

The "Accountant" you asked about is one item in a definition list, so you'd probably want to loop through that list with another .each() statement nested inside the one above.

You don't make it clear how you want to use the values, but this should get you started. For further details about the various jQuery methods I've mentioned check the jQuery API.

于 2012-07-10T07:32:57.840 に答える
5

To get "PersonA": $('#v-0 h2 a').html();

To get href of that link: $('#v-0 h2 a').attr('href');

To get "Accountant": $('#v-0 dl dd').html();

You can modify the id ("v-0") at the start of the selector to choose a particular "row" of your data set.

于 2012-07-10T07:33:52.403 に答える
2
document.getElementById(Id).value

returns value of element with specific id. in jquery:

$("#id").val()

by class $(".yourClass").val()

to get attribute value use attr("attributeName") for example $(".class1").attr('href').

if you want to get text from specified element use .text() like $(".title").text() //will return Accountant.

于 2012-07-10T07:32:59.100 に答える
0

You mean selecting them with a jQuery selector? That would be done like so:

$('.class1').attr('href') //class1 href, i persume you dont mean classA as it doesnt exist in your code
$('.class1').text(); //PersonA text using the same selector
$('.title').text(); //Accountant from the .title dd
于 2012-07-10T07:30:02.060 に答える