admin管理员组文章数量:1335606
I have some code that does almost exactly what I want. It changes all the strong tags to styled h3 tags, which is perfect, but for the life of me I can't figure out what to replace ".click" with to make it run automatically. I tried ".ready" and it gives me an error in my jquery.
$(document).ready(function(){
$('strong').click(function(){
$(this).replaceWith($('<h3 style="margin:0px;display:inline;">' + this.innerHTML + '</h3>'))
})
});
I have some code that does almost exactly what I want. It changes all the strong tags to styled h3 tags, which is perfect, but for the life of me I can't figure out what to replace ".click" with to make it run automatically. I tried ".ready" and it gives me an error in my jquery.
$(document).ready(function(){
$('strong').click(function(){
$(this).replaceWith($('<h3 style="margin:0px;display:inline;">' + this.innerHTML + '</h3>'))
})
});
Share
Improve this question
asked Jun 3, 2015 at 20:58
horribly_n00biehorribly_n00bie
791 silver badge12 bronze badges
1
- What error do you get? – Alex McMillan Commented Jun 3, 2015 at 20:59
4 Answers
Reset to default 5I'd suggest just going straight to replaceWith()
:
// most (if not all) jQuery methods iterate over
// the collection to which they're chained:
$('strong').replaceWith(function () {
// here 'this' is the individual <strong> element over
// which the method iterates, and we return the created element:
return $('<h3 style="margin:0px;display:inline;">' + this.innerHTML + '</h3>');
});
$('strong').replaceWith(function () {
return $('<h3 style="margin:0px;display:inline;">' + this.innerHTML + '</h3>');
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong>1</strong>
<strong>2</strong>
<strong>3</strong>
<strong>4</strong>
Incidentally, in plain JavaScript, this is also quite easily possible:
// creating a <h2> element:
var heading = document.createElement('h2'),
// initialising an empty variable:
clone;
// setting the display and margin properties:
heading.style.display = 'inline';
heading.style.margin = '0';
// using Function.prototype.call() to use
// Array.prototype.forEach() on the array-like
// NodeList returned by document.querySelector():
Array.prototype.forEach.call(document.querySelectorAll('strong'), function(bold) {
// cloning the created <h2> element:
clone = heading.cloneNode();
// setting its innerHTML:
clone.innerHTML = bold.innerHTML;
// traversing to the parentNode, and
// using Node.replaceChild() to replace
// existing <strong> element with the
// cloned <h2> element:
bold.parentNode.replaceChild(clone, bold);
});
var heading = document.createElement('h2'),
clone;
heading.style.display = 'inline';
heading.style.margin = '0';
Array.prototype.forEach.call(document.querySelectorAll('strong'), function(bold) {
clone = heading.cloneNode();
clone.innerHTML = bold.innerHTML;
bold.parentNode.replaceChild(clone, bold);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong>1</strong>
<strong>2</strong>
<strong>3</strong>
<strong>4</strong>
References:
- JavaScript:
Element.innerHTML
.Node.cloneNode()
.Node.parentNode
.Node.replaceChild()
.
- jQuery:
replaceWith()
.
You want to use each()
so it iterates over all of them.
$('strong').each(function(){ ... });
What you are doing is adding a handler to all the <strong>
elements on the page that will trigger whenever one of them is clicked. If you want to perform the replace as soon as the document is ready try this:
$(document).on('ready', function () {
$('strong').each(function () {
$(this).replaceWith($('<h3 style="margin:0px;display:inline;">' + this.innerHTML + '</h3>'));
});
});
This should do it.
$(document).ready(function(){
$('strong').each(function(){
$(this).replaceWith($('<h3 style="margin:0px;display:inline;">' + this.innerHTML + '</h3>'))
})
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong>1</strong>
<strong>2</strong>
<strong>3</strong>
本文标签: javascriptChange a dom using replaceWithStack Overflow
版权声明:本文标题:javascript - Change a dom using .replaceWith - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742392558a2466278.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论