javascript - Get content of DT and DD elements with JQuery
问题描述
I am trying to get the contents of an Dictionary Term (DT) and it's Dictionary Definition (DD) by clicking a link.
The steps taken so far:
- set link's data-track property to DT id
- construct selector
- use selector to get DT Successful so far, but then...
- update selector to get DD
This is where it fails: the step using the updated selector does not return the contents of the Dictionary Definition
Tried constructing selectors with "dd", "dd:first", and "dd:nth-child(1)"
and changing "var definition = $(defSelector).text();" to "...val()" and "..html()" but none of those worked.
Please let me know if you can identify and explain a solution.
$('.help-lookup').click(function(){
// get this data-target
var target = $(this).data("target");
alert('data target = ' + target);
//create # + DT selector with target value
selector = ('#'+target);
alert('selector= '+ selector);
//get tile
var title = $(selector).html();
//title = $(this).text( title );
alert (title);
// create DD selector
var defSelector = (selector+' dd:first');
alert(defSelector);
//get definition
var definition = $(defSelector).text();
alert(definition);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<p>
Goal: clicking "look up" returns html contents of DT and DL<br/>
Expect: final alert to display: "Definition text"<br/>
Actual: final alert displays ""
</p>
<a href="#" data-target="TermToLookUp" class="help-lookup">look up</a>
<dl>
<dt id="TermToLookUp" class="anchor">Term</dt>
<dd>Defintion text</dd>
</dl>
解决方案
(selector+' dd:first')
is wrong. You are trying to get the text of the element that is right next to the selector
(#TermToLookUp
). So use the .next()
method.
$('.help-lookup').click(function(){
// get this data-target
var target = $(this).data("target");
alert('data target = ' + target);
//create # + DT selector with target value
selector = ('#'+target);
alert('selector= '+ selector);
//get tile
var title = $(selector).html();
//title = $(this).text( title );
alert (title);
// create DD selector
var defSelector = $(selector).next("dd"); // Change here
alert(defSelector);
//get definition
var definition = defSelector.text(); // Change here
alert(definition);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<p>
Goal: clicking "look up" returns html contents of DT and DL<br/>
Expect: final alert to display: "Definition text"<br/>
Actual: final alert displays ""
</p>
<a href="#" data-target="TermToLookUp" class="help-lookup">look up</a>
<dl>
<dt id="TermToLookUp" class="anchor">Term</dt>
<dd>Defintion text</dd>
</dl>
推荐阅读
- c# - 在蓝色和绿色之间徘徊会产生灰色?Unity, 2d Sprite.color
- r - 为多级混合效应模型汇总(和最大化)数据
- javascript - MERN Stack:使用 Redux 存储用户是否登录
- ssas - 基于strtomember访问前一个成员
- javascript - 在 express 中将绝对 url 路径传递给车把视图
- swift - 结合 Semaphore 来限制 .notify 的异步请求和调度组与 defer 和 App 冻结
- android - 亚行以退出代码 1 退出
- java - 如何在 Java 中使用 BufferedReader 在 .txt 文件中查找特定字符串
- java - 错误:由 OutputStream.flush() 引起的“nativeDrain 中没有错误”
- c# - 在一个时间点监听两个事件的方法的执行时间c#