说我有类似的东西:
<div class="myawesomeclass" id="myTag"><strong>Lots of stuff</strong></div>
理想情况下,我希望最终得到两个变量,其中包含开始和结束标记的字符串值:
OpeningTag: CloseingTag:
如何使用JQuery执行此操作?
答案 0 :(得分:1)
您可以使用javascripts outerHTML
属性来获取标记的HTML,然后使用内部HTML作为分隔符将其拆分,这是一个示例:
var tag = $('#myTag'),
outerHTML = tag[0].outerHTML,
innerHTML = tag.html(),
result = outerHTML.split(innerHTML),
openingTag = result[0],
closingTag = result[1];
$('.oT').text(openingTag);
$('.cT').text(closingTag);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myawesomeclass" id="myTag"><strong>Lots of stuff</strong>
</div>
<div class="result">
opening Tag: <span class="oT"></span><br>
closing Tag: <span class="cT"></span>
</div>