【发布时间】:2019-04-22 20:46:36
【问题描述】:
我想使用 JavaScript 找到 div 中的第一个链接(了解更多链接),并使用该 href 动态包装整个 div。
我怎样才能修改这段代码,让它这样做?
$('div.textPane .DNNModuleContent.ModDNNHTMLC').wrap("<a href=''></a>", function() {
a.href = $('a:first', this).attr('href');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="dnn_pane02textLeft" class="flexPane LeftPane textPane">
<div class="DnnModule DnnModule-DNN_HTML DnnModule-499 DnnVersionableControl">
<a name="499"></a>
<div id="dnn_ctr499_ContentPane" style="background-color:#f2a42a;">
<div id="dnn_ctr499_ModuleContent" class="DNNModuleContent ModDNNHTMLC">
<div id="dnn_ctr499_HtmlModule_lblContent">
<h1>Analytic Solutions</h1>
<p>Wilshire serves the investment analytic needs of institutional investors by offering a platform that combines the depth of our capabilities within...
</p>
<h5><a href="/analytics/analytic-solutions/">Learn More</a></h5>
</div>
</div>
</div>
</div>
</div>
【问题讨论】:
-
您需要在问题中添加相关代码 - 不要指望我们走出 SO 才能看到它。
-
请附上MCVE。
-
从什么时候开始,JS fiddle外部链接就一直用在SO上?我添加了代码@Andy
-
他们有,但总是伴随着代码 - 如果 jsfiddle 不可用,你的问题毫无价值。
标签: javascript jquery href