我一直在尝试学习如何为我的项目使用 expect.js 断言插件。我的问题是我想获取仅显示在浏览器检查元素的控制台部分上的错误消息,以便在我单击按钮时向我的 HTML 页面发出警报。这是我到目前为止一直在做的事情
<body>
<script>mocha.setup('bdd')</script>
<div id="mocha"></div>
<ol>
<li>tes</li>
<li>tes</li>
<li>tes</li>
<li>tes</li>
<label></label>
<div class="post">
</div>
<button id="klik">Klik</button>
</ol>
<script type="text/javascript">
var salah="";
describe("Index", function () {
it("should have div",function(){
$expect('input').to.exist(function (willThrow) {
// Some cleanup code.
return 'Please add a input to the page.';
});
});
it("should have img",function()
{
$expect('img').to.exist('Please add a img to the page!');
});
it("should have li 4",function()
{
$expect('ol > li').to.have.items(4);
});
});
mocha.run();
var fn = function(){
var missingPorHMsg = "Your div should surround the h2 and two p's";
$expect('div.post').to.exist('Make sure your new div has a class name of "post"!')
.and.to.have.children('h2', missingPorHMsg)
.and.to.have.children('p', missingPorHMsg)
.that.has.elements(4, missingPorHMsg).end()
.and.to.have.css( 'border'
, '1px dashed #ccc'
, "Make sure you give your post div a border!");
};
$('#klik').click(function(e){
alert(fn());
});
</script>
</body>
当我单击按钮时,当我从浏览器检查检查元素时,错误将显示在控制台上。我想要做的是在我单击按钮时收到错误消息并提醒消息。